Try   HackMD

2020/7/9 Cocoaheads Zonble 講述

  • 在 Objc 裡的 Category 包成 SDK 的時候,在 Swift 看不到是為什麼?

    • 要在 Xcode build setting 加上 -objc 才會把 Category 包進去
  • NSNumber 繼承自什麼?

    • NSValue
    • 為什麼 NSInteger 不能放到 NSArray 去?(這題請涵宇回答,Challenge from Shiva)
      • 因為 NSInteger 不是 Object,NSArray 只能放 Object。(NSInteger 沒有 *)
  • 什麼是 header search paths?

    • 編譯 C, Objective-C, C++ 或 Objective-C++ 時要讓編譯器搜尋需要被 import 的 header 檔清單
    • 在早期開發時,Library 只會發佈 .a 檔及多個 header,此時需要加入 LIBRARY_SEARCH_PATHSHEADER_SEARCH_PATHS,讓 IDE 知道需要額外搜尋的目錄
    • 另外,在 macOS App 可能會用到如 /usr/local 或是 /AppleInternal/Library 內的 dyld, bin 檔或 Framework,如果沒有在 LD_RUNPATH_SEARCH_PATHS 加入搜尋目標,在軟體啟動時會馬上報 Image not found 的 crash
  • 選用 Objc 當 SDK 的開發語言,主要是能穩定又安全的讓 Objc/Swift 呼叫。因為 Swift 的 Struct 無法被 Objc 呼叫。我們無法決定客戶要用 Objc 甚至是哪一版的 Swfit。因此採用 Objc 是最好的。

  • 什麼是 linker?(題目記不清楚)

  • Nil/nil/NSNull 差別在哪?

    • Nil 跟 nil 都是指向一位空的記憶體位置,而 Nil 是指 Class 的空,nil 是指 instance 的空
      • 記憶體位置在哪?
        • 記憶體位置在 0
    • NSNull 是 JSON 裡面的 nil value 呈現的方式
    • NULL 是在 C header 裡的 define SO
  • 除了 Nil/nil/NSNull 之外還有什麼方式可以表達沒有值?

  • AVFoundation 的底層有一些是用 WebKit 實作的,有多少不確定

  • WebKit 是 Open source 的專案,從第一台 iPhone 就有了。

  • NSString *text = @"hello";, "hello" 的 retain count 是多少?

    • Retain count 的最大值(應該是 NSNotFound) https://developer.apple.com/documentation/foundation/nsnotfound
    • Objc 在編譯階段會把所有的 literal 編好放在某個位置,因為是程式碼區段,不會被釋放,可是因為是 NSObject,必然還是要有 Retain count, 所以被設定為 NSNotFound(2^64-1)
  • NSString *text = @"\0"; 會發生什麼事?文字長度是多少?

    • 1。
  • 一堆 queue 跟 thread 的討論

  • 在 Objc 使用 get 開頭的 method 是指要拿 C 層級的資料,這是 Objc 的 convention。Objc 自己的 getter 不會用 get 開頭。

  • Swift Optimization Level 有哪幾種優化等級?差別在哪?

    • Optimization Level: None [-Onone]
    • Optimization Level: Fast [-O]
    • Optimization Level: Fast,WMO [-O -whole module optimization]
    • 編譯器會把一些常數直接加起來,或是把一些泛型做客製的處理。Fast 就是只專注一個檔案的優化。而 Fast,WMO 是把全部專案的 swift 放在同一個 file 再去優化。
    • -Osize

這邊的 optimization 看起來已經變預設值了,現在在 clang 和 Swift compiler 則有不同的 opimization 選項。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Whole-module optimization can be enabled with the -whole-module-optimization (or -wmo) compiler flag, and in Xcode 8 it is turned on by default for new projects.
https://swift.org/blog/whole-module-optimizations/
https://swift.org/blog/osize/

  • Swift 的 Void 真實身分是什麼?

  • Objc 的 static variable 存在哪裡?

    • 只存在這個 class。
  • Objc 的 Block 能不能放進 NSArray?

    • 可以,因為 Block 是物件。
  • Objc 的 Block 能不能寫遞迴?

    • 不行,會 crash
    • 因為 block 執行完成之前,變數尚未被賦值,是 nil, 呼叫會 Crash.
    • 下面這段可以執行??? (請涵宇研究 😜)
    ​​​​int(^__block test)(int) = ^int(int i) {
    ​​​​    if (i < 1) {
    ​​​​        return -1;
    ​​​​    } else if (i <= 2) {
    ​​​​        return 1;
    ​​​​    }
    ​​​​    return test(i - 1) + test(i - 2);
    ​​​​};
    
    ​​​​for (int i = 1; i < 10; i++) {
    ​​​​    printf("%i ", test(i));
    ​​​​}
    
  • Swift 的 closure 能不能寫遞迴?

    ​​​​
    ​​​​// 可以執行的方式
    ​​​​var worker = {}
    ​​​​var count = 1
    ​​​​worker = {
    ​​​​    print(count)
    ​​​​    count += 1
    ​​​​    if count > 10 {return}
    ​​​​    worker()
    ​​​​}
    ​​​​worker()
    
    ​​​​// 不能執行的版本
    ​​​​let worker = {
    ​​​​    worker() // Compile error: Variable used within its own initial value
    ​​​​}
    
  • DataSource 跟 Delegate 最關鍵的差異是什麼?