蘋果官方文件對 Foundation 框架的描述多達兩百餘篇,直接從頭讀完既不現實也沒效率。以下依功能主題重新整理成一份查閱地圖,方便在需要時快速定位(統計基準約為 2014 年 5 月的官方文件清單)。
本內容遵循「姓名表示-非商業性使用-相同方式分享」創用條款;轉載請保留出處:太陽火神的美麗人生——本部落格專注於敏捷開發及行動與物聯網裝置研究:iOS、Android、Html5、Arduino、pcDuino。
先看這幾篇建立全局觀念:Foundation Constants Reference(常量)、Foundation Data Types Reference(資料型別)、Foundation Framework Reference(框架總覽)、Foundation Functions Reference(全域函式)、以及各版本的 Release Notes。
NSObject 及其協定是所有 Objective-C 類別的祖先;另有 NSNull、NSValue、NSUUID、NSMethodSignature,以及 NSCopying/NSMutableCopying 這組複製協定。記憶體管理相關可參考 Advanced Memory Management Programming Guide 與 NSAutoreleasePool。
Binary Data Programming Guide、NSData、NSMutableData。
Date and Time Programming Guide、NSDate、NSDateComponents、NSDateFormatter(含網際網路日期格式)、NSLocale、NSTimeZone、NSCalendar。處理跨時區與各地曆法時,這一組是必讀。
Number and Value Programming Topics、NSNumber、NSDecimalNumber(搭配 NSDecimalNumberBehaviors 協定與 NSDecimalNumberHandler)、NSIndexPath。需要精確十進位運算(例如金額)時,NSDecimalNumber 比 double 可靠得多。
String Programming Guide、NSString/NSMutableString、Attributed String Programming Guide、NSAttributedString/NSMutableAttributedString,以及轉為組合式 Unicode 字串的說明。
NSCharacterSet、NSMutableCharacterSet。常搭配 decimalDigitCharacterSet、whitespaceAndNewlineCharacterSet 做輸入驗證。
NSEnumerator、NSFastEnumeration 協定與 Enumeration Sample。Objective-C 的 for (x in collection) 就是建立在 NSFastEnumeration 之上。
Data Formatting Guide、NSFormatter(抽象基底)、NSDateFormatter、NSNumberFormatter、NSByteCountFormatter(把位元組數轉成人類可讀的檔案大小)、NSScanner、NSSortDescriptor。
Collections Programming Topics 是總入口。實作類別包含 NSArray/NSMutableArray、NSSet/NSMutableSet、NSOrderedSet/NSMutableOrderedSet、NSIndexSet/NSMutableIndexSet、NSCountedSet,以及 NSDictionary/NSMutableDictionary。另有 NSPointerArray、NSHashTable、NSMapTable 這三個可存放弱參照或任意指標的特殊集合,Property List Programming Guide 與 NSPropertyListSerialization 則負責 plist 的讀寫。
Exception Programming Topics、NSException、NSError、NSErrorRecoveryAttempting 協定、NSAssertionHandler。原則上:程式錯誤用例外,可預期的執行期失敗(檔案不存在、網路中斷)用 NSError。
Key-Value Coding 與 Key-Value Observing 兩份 Programming Guide,加上 NSKeyValueCoding/NSKeyValueObserving 協定。KVO 是 Cocoa 綁定與多個現代框架的反應式基礎。
Notification Programming Topics、NSNotification、NSNotificationCenter、NSNotificationQueue。
Predicate Programming Guide、NSPredicate、NSComparisonPredicate、NSCompoundPredicate、NSExpression,以及 NSRegularExpression、NSDataDetector。NSPredicate 除了用正則驗證,也能直接對陣列做 filteredArrayUsingPredicate: 過濾。
NSJSONSerialization、NSXMLParser 與 NSXMLParserDelegate、Event-Driven XML Programming Guide、XML 效能說明。
Undo Architecture、NSUndoManager、SimpleUndo。
Timer Programming Topics、NSTimer。
NSThread、NSRunLoop、NSOperation/NSOperationQueue/NSBlockOperation/NSInvocationOperation,以及 NSCondition、NSConditionLock、NSLock、NSRecursiveLock 與 NSLocking 協定。跨執行緒溝通則看 NSPort 系列(NSMachPort、NSMessagePort)、NSPipe 與 NSProcessInfo。
URL Loading System Programming Guide 是入口。核心類別為 NSURL、NSURLRequest/NSMutableURLRequest、NSURLResponse 系列、NSURLConnection 及其三組 delegate 協定,以及較新的 NSURLSession(含 Data/Upload/Download Task 與各自 delegate)、NSURLSessionConfiguration。認證相關有 NSURLAuthenticationChallenge、NSURLCredential、NSURLCredentialStorage、NSURLProtectionSpace;快取則是 NSURLCache、NSCache。範例與最佳實務可參考 AdvancedURLConnections、CustomHTTPProtocol、HTTPS Server Trust Evaluation、Resumable Downloads 與歷屆 WWDC 的網路主題。
Preferences and Settings Programming Guide、NSUserDefaults。
Resource Programming Guide、Bundle Programming Guide、NSBundle、NSFileManager 與 NSFileManagerDelegate。
Archives and Serializations Programming Guide、NSCoder/NSCoding/NSSecureCoding、NSKeyedArchiver/NSKeyedUnarchiver(含 delegate)、NSFileHandle、NSFileWrapper、NSFileCoordinator、NSFileSecurity、NSDatairectoryEnumerator,以及 NSMetadataQuery 系列與 NSFileVersion。
Stream Programming Guide、NSStream 與 NSStreamDelegate、NSInputStream/NSOutputStream。
NSNetService、NSNetServiceBrowser 及各自 delegate,另有 Bonjour over Bluetooth 的說明與 WiTap 範例。
NSOrthography、NSTextCheckingResult、NSLinguisticTagger、NSValueTransformer、NSProgress、NSPurgeableData、NSDiscardableContent、NSUbiquitousKeyValueStore 等。iCloud 相關可看歷屆 WWDC 的 iCloud Storage 主題。
實務上,多數日常需求集中在少數幾篇:集合類別、字串、日期與 NSDateFormatter、NSFileManager、NSUserDefaults、NSURLSession 與 NSPredicate。把這幾塊讀熟,其餘遇到再查即可。另外提醒,官方文件近年已大量改版並整合進 Xcode 的 Quick Help,查閱時建議以 Xcode 內建文件或 developer.apple.com 的最新版本為準,本清單僅作為主題索引使用。