Skip to content

Instantly share code, notes, and snippets.

func B(callback: @escaping (Bool) -> Void) {
//step2
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 100) {//this 100s indicates long time task
//step 3
callback(true)
}
}
//step1
func B {
//ex: 比較 a,b大小
var a = 54
var b = 5
//用function:
func compare(val1: Int,val2: Int) -> Bool {
 return a > b
}
assert(compare(val1:a,val2:b), "error, a can not larger than b")
//用closure:
assert(a > b, "error, a can not larger than b") //note: a > b 即為closure,其省略了參數和回傳值
//用語言提供的基本語法
func test1(arr: [Int]) -> Void {
 for item in arr {
 if isOdd(val: item) {
 print("\(item)")
 }
 }
}
func test2(arr: [Int]) -> Void {
 for item in arr {
//用語言提供的基本語法找出陣列中的奇數和偶數
func test1(arr: [Int]) -> Void {
for item in arr {
if isOdd(val: item) {
print("\(item)")
}
}
}
func test2(arr: [Int]) -> Void {
for item in arr {
@tgnivekucn
tgnivekucn / gist:e93bc51a4d651c6ef7b560e9c7644d21
Last active December 3, 2021 13:23
IOS combine framework sample
import UIKit
import Combine
class ViewModel {
var updateLabel: ((String?) -> Void)?
var textString: String? {
didSet {
updateLabel?(textString)
}
}
@tgnivekucn
tgnivekucn / Singleton.swift
Created July 17, 2022 11:09
Singleton pattern in Swift
// lazy Singleton: create the singleton object when needed
// Note: Global constants and variables are always computed lazily
// ref: https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID263
class SingletonGreed {
static let shared = SingletonGreed()
private init() {}
}
class SingletonThreadSafe {
private static var shared: SingletonThreadSafe?
// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
@tgnivekucn
tgnivekucn / gist:58b48eae9d1fbaf7e83daf08fcf52915
Created August 2, 2022 03:21
Custom color identifier rule
Color identifier naming rule:
Regular expression:
(color)\.((common)|([A-Za-z])+(Page)){1}(\.([A-Za-z])+)*(\.([A-Za-z])+(Color)s?)$
Example:
1. Color category
2. UI page name or plist file name
3. Subview name(ex: customCell) (p.s. this field might be zero)
4. Unique color name (p.s. or unique color array name)
func validatePhoneNumber(phoneNumber: String) -> Bool {
if let regex = try? NSRegularExpression(pattern: "09\\d{2}-\\d{3}-\\d{3}") {
let range = NSRange(location: 0, length: phoneNumber.utf16.count)
let result = regex.firstMatch(in: phoneNumber, range: range)
if result != nil {
return true
}
}
return false
}