This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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,其省略了參數和回傳值 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//用語言提供的基本語法 | |
func test1(arr: [Int]) -> Void { | |
for item in arr { | |
if isOdd(val: item) { | |
print("\(item)") | |
} | |
} | |
} | |
func test2(arr: [Int]) -> Void { | |
for item in arr { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//用語言提供的基本語法找出陣列中的奇數和偶數 | |
func test1(arr: [Int]) -> Void { | |
for item in arr { | |
if isOdd(val: item) { | |
print("\(item)") | |
} | |
} | |
} | |
func test2(arr: [Int]) -> Void { | |
for item in arr { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Combine | |
class ViewModel { | |
var updateLabel: ((String?) -> Void)? | |
var textString: String? { | |
didSet { | |
updateLabel?(textString) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
OlderNewer