Skip to content

Instantly share code, notes, and snippets.

@marslin1220
marslin1220 / mutation.swift
Created October 16, 2020 03:13
Mutation with Struct & Class
struct AStruct {
let letRefValue = "foo"
var varRefValue = "bar"
let letDataValue = 0
var varDataValue = 0
func changeLetRefValue() {
letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
}
/**
* @protocol XCTMetric
* Defines a protocol which may be used with the -measureWithMetrics* methods on XCTestCase.
*
* @discussion
* Classes conforming to XCTMetric must also adopt NSCopying, as a unique metric instance is copied for each iteration.
*/
public protocol XCTMetric : NSCopying, NSObjectProtocol {
// 1.
@available(iOS 13.0, *)
func testMeasureSPFCascadeScrollingAsAnXtraUser() {
let options = XCTMeasureOptions()
options.invocationOptions = [.manuallyStart, .manuallyStop] //< 2.
// 3.
measure(metrics: [SlowFrameMetric()], options: options) {
app.launch()
// 1.
@available(iOS 13.0, *)
func testMeasureSPFCascadeScrollingAsAnXtraUser() {
// 2.
measure(metrics: [SlowFrameMetric()]) {
app.launch()
// interact with the app, like scrolling or swiping
app.terminate()
import Foundation
import XCTest
import QuartzCore
// 1.
@available(iOS 11.2.0, *)
class SlowFrameMetric: NSObject, XCTMetric {
private static let slowFrameThresholdInMiliSecond = 0.017
private let frameTimeintervalCollector = FrameTimeintervalCollector()
import Foundation
class FrameTimeintervalCollector {
private(set) lazy var displayLink: CADisplayLink = { //< 1.
let displayLink = CADisplayLink(target: self, selector: #selector(tick(displayLink:)))
displayLink.add(to: .main, forMode: .default)
return displayLink
}()
private(set) var frameTickTimeSet = Set<FrameTickTime>() //< 2.
private var lastTimestamp: TimeInterval = 0
public class DramaInfo: NSObject, Codable {
@objc public let currentEpisode: Int
@objc public let episodeID: String
@objc public let name: String
@objc public let viewCount: Int
@objc public let totalEpisode: Int
enum CodingKeys: String, CodingKey {
case currentEpisode = "currentEps"
case episodeID = "id"
@marslin1220
marslin1220 / SelfPrinter.swift
Created June 25, 2018 14:43
Print swift script like a type machine by script self.
#!/usr/bin/swift
//
// SelfPrinter.swift
// SelfPrinter
//
// Created by Lin Cheng Lung on 2018/6/25.
// Copyright © 2018 Lin Cheng Lung. All rights reserved.
//
import Foundation
@marslin1220
marslin1220 / pre-commit-clang-format
Last active July 8, 2017 02:07 — forked from shiva/pre-commit-clang-format
Git pre-commit hook that invokes clang-format to reformat C/C++/Objective-C source code.
#!/bin/bash
# git pre-commit hook that runs an clang-format stylecheck.
# Features:
# - abort commit when commit does not comply with the style guidelines
# - create a patch of the proposed style changes
# modifications for clang-format by rene.milk@wwu.de
# This file is part of a set of unofficial pre-commit hooks available
# at github.
@marslin1220
marslin1220 / RequestPlayground.swift
Last active August 28, 2023 02:33
How to create a HTTP GET request on Swift Playground
import Foundation
import PlaygroundSupport
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
PlaygroundPage.current.needsIndefiniteExecution = true
// Refer to the example: https://grokswift.com/simple-rest-with-swift/
let todoEndpoint: String = "https://jsonplaceholder.typicode.com/todo/1"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")