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
protocol PageViewControllerDelegate: class { | |
/// 設定頁數 | |
/// | |
/// - Parameters: | |
/// - pageViewController: _ | |
/// - numberOfPage: _ | |
func pageViewController(_ pageViewController: PageViewController, didUpdateNumberOfPage numberOfPage: Int) | |
/// 當 pageViewController 切換頁數時,設定 pageControl 的頁數 |
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
var dataList: [Int] = [1, 2, 3] | |
func filter(dataList: [Int], function: (Int) -> Bool) -> [Int]? { | |
var result: [Int]? | |
for i in dataList { | |
// 傳值給 block,並接收 block 回傳的值 | |
if function(i) { | |
if result == nil { result = [Int]() } | |
result?.append(i) | |
} |
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
class Man { | |
var closure: (() -> Void)? | |
var name: String = "Frank" | |
init() { | |
closure = { [weak self] in | |
guard let self = self else { return } | |
print("Init with \(self.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
class Man { | |
var closure: (() -> Void)? | |
var name: String = "Frank" | |
init() { | |
closure = { print("Init with \(self.name)") } | |
} | |
deinit { | |
print("Deinit") |
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
// 建立 serial queue,預設即是 serial queue | |
let serialQueue = DispatchQueue(label: "serialQueue") | |
// 建立非同步的 block,非同步狀態下,才能測試 serial 和 concurrent 的差異 | |
serialQueue.async { | |
} |
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
var count = 5 | |
func mcl(k: Int) -> Int { | |
if k == 1 { | |
return 1 | |
} | |
return k * mcl(k: k - 1) | |
} |
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
// 1、3、5、7、9 等差數列合 | |
print("========== 1、3、5、7、9 等差數列合 ==========") | |
var count = 9 | |
func ap(j: Int) -> Int { | |
if j == 1 { | |
return 1 | |
} | |
return j + ap(j: j - 2) |
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
// 1~10 累加 | |
print("================ 1~5累加 ================") | |
var count = 5 | |
func add(i: Int) -> Int { | |
if i == 1 { | |
return 1 | |
} | |
return i + add(i: i - 1) |
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 a() -> Void { | |
print(“a()...”) | |
} | |
func b(closure: () -> Void) { | |
closure() // call 傳入的 func | |
} | |
// 將 a func 當成參數傳給 b func | |
b(closure: a) // Prints "a()..." |
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 test() { | |
print("test...") | |
} | |
let testFunc = test // 將 func 存放在變數中 | |
testFunc() // Prints "test..." |
NewerOlder