第一部分 Cocoa概述
第1 章 Cocoa 與Mac OS X ............................................................................2
1.1 理解何時該用Cocoa ..................................................................................................2
1.1.1 Carbon ................................................................................................................3
1.1.2 Java .....................................................................................................................4
1.1.3 Cocoa .................................................................................................................5
1.1.4 UNIX API ..........................................................................................................6
1.1.5 其他選擇 ............................................................................................................7
1.2 理解Cocoa 在Mac OS X 中的角色 ..........................................................................7
1.2.1 Cocoa .................................................................................................................8
1.2.2 Quartz .................................................................................................................9
1.2.3 Core Foundation ...............................................................................................11
1.2.4 Darwin ..............................................................................................................12
1.2.5 XNU .................................................................................................................13
1.3 概覽 ...........................................................................................................................14
第2 章 Cocoa 的可選語言 .............................................................................15
2.1 麵嚮對象 ...................................................................................................................15
2.2 Objective-C ................................................................................................................17
2.2.1 Objective-C 的可選編譯器 .............................................................................18
2.2.2 與Java 及C++ 的區彆 ...................................................................................19
2.2.3 Objective-C 2.0 ................................................................................................21
2.3 Ruby 與Python .........................................................................................................22
2.4 小結 ...........................................................................................................................22
第3 章 使用Apple 的開發工具 ......................................................................23
3.1 獲取Apple 的開發工具 ............................................................................................24
3.2 Interface Builder ........................................................................................................24
3.2.1 Outlet 與Action ...............................................................................................26
3.2.2 Cocoa 綁定 ......................................................................................................27
3.2.3 繪製簡單的應用程序 ......................................................................................28
3.3 Xcode .........................................................................................................................31
3.3.1 創建簡單的項目 ..............................................................................................31
3.3.2 OpenStep Bundle .............................................................................................33
3.3.3 開發樣例 ..........................................................................................................34
3.3.4 不用Xcode 來編譯 .........................................................................................35
3.4 Objective-C ................................................................................................................36
3.4.1 為何學習Objective-C .....................................................................................37
3.4.2 對C 的添加 .....................................................................................................37
3.4.3 異常與同步 ......................................................................................................44
3.4.4 自省 ..................................................................................................................47
3.4.5 C 中的Objective-C .........................................................................................50
3.4.6 Objective-C 2.0 ................................................................................................51
3.4.7 Block ................................................................................................................54
3.4.8 Objective-C++ ..................................................................................................56
3.5 Cocoa 規約 ................................................................................................................57
3.5.1 命名 ..................................................................................................................57
3.5.2 內存管理 ..........................................................................................................60
3.5.3 構造器與初始化器 ..........................................................................................62
3.6 小結 ...........................................................................................................................64
第二部分 Cocoa框架
第4 章 Foundation: Objective-C 基礎庫 ........................................................66
4.1 一般概念 ...................................................................................................................67
4.1.1 可變的 ..............................................................................................................67
4.1.2 Class Cluster ....................................................................................................67
4.2 Core Foundation 類型 ...............................................................................................71
4.3 基本數據類型 ...........................................................................................................72
4.3.1 非對象類型 ......................................................................................................72
4.3.2 字符串 ..............................................................................................................73
4.3.3 數字和值的裝箱 ..............................................................................................75
4.3.4 數據 ..................................................................................................................76
4.3.5 緩存和丟棄數據 ..............................................................................................77
4.3.6 日期和時間 ......................................................................................................78
4.4 容器 ...........................................................................................................................79
4.4.1 比較和排序 ......................................................................................................79
4.4.2 原生容器 ..........................................................................................................82
4.4.3 數組 ..................................................................................................................82
4.4.4 字典 ..................................................................................................................84
4.4.5 集閤 ..................................................................................................................84
4.5 枚舉類型 ...................................................................................................................85
4.5.1 利用高級消息枚舉 ..........................................................................................87
4.5.2 利用block 枚舉 ...............................................................................................89
4.5.3 支持快速枚舉 ..................................................................................................90
4.6 屬性列錶 ...................................................................................................................95
4.6.1 序列化 ..............................................................................................................96
4.6.2 用戶默認值 ......................................................................................................98
4.7 和文件係統交互 .....................................................................................................101
4.7.1 Bundle ............................................................................................................101
4.7.2 工作區和文件管理 ........................................................................................104
4.7.3 使用路徑工作 ................................................................................................106
4.7.4 文件訪問 ........................................................................................................106
4.8 通知 .........................................................................................................................107
4.8.1 請求通知 ........................................................................................................108
4.8.2 發送通知 ........................................................................................................109
4.8.3 發送異步通知 ................................................................................................110
4.8.4 分布式通知 ....................................................................................................113
4.9 小結 .........................................................................................................................114
第5 章 應用程序相關概念 ............................................................................ 115
5.1 Run Loop .................................................................................................................115
5.2 應用程序和委托 .....................................................................................................122
5.3 響應者鏈 .................................................................................................................123
5.3.1 事件傳遞 ........................................................................................................124
5.3.2 目標與活動 ....................................................................................................127
5.3.3 變成首先響應者 ............................................................................................128
5.4 應用程序裏的Run Loop ........................................................................................128
5.5 委托與通知 .............................................................................................................131
5.6 視圖層次 .................................................................................................................134
5.6.1 窗口 ................................................................................................................134
5.6.2 視圖 ................................................................................................................135
5.6.3 格子 ................................................................................................................136
5.7 總結 .........................................................................................................................137
第6 章 創建圖形用戶界麵 ............................................................................138
6.1 位置視圖 .................................................................................................................138
6.2 嵌套視圖 .................................................................................................................143
6.2.1 剪裁視圖 ........................................................................................................143
6.2.2 滾動視圖 ........................................................................................................144
6.2.3 頁簽視圖 ........................................................................................................145
6.2.4 分割視圖 ........................................................................................................146
6.2.5 方框 ................................................................................................................147
6.3 創建視圖 .................................................................................................................148
6.3.1 按鈕 ................................................................................................................148
6.3.2 文本視圖 ........................................................................................................150
6.3.3 數據視圖 ........................................................................................................150
6.3.4 菜單 ................................................................................................................158
6.4 Cocoa 綁定 ..............................................................................................................160
6.4.1 鍵—值編碼 ....................................................................................................160
6.4.2 鍵—值觀測 ....................................................................................................165
6.4.3 公開綁定 ........................................................................................................168
6.4.4 通用控製器 ....................................................................................................169
6.4.5 綁定與用戶默認值 ........................................................................................170
6.4.6 在Interface Builder 中使用綁定 ..................................................................170
6.5 小結 .........................................................................................................................176
第7 章 窗口與菜單 .......................................................................................177
7.1 理解窗口 .................................................................................................................178
第8 章 Cocoa 中的文本 ...............................................................................181
8.1 字符串的構建與析構 .............................................................................................182
8.2 字符串標注 .............................................................................................................184
8.3 本地化 .....................................................................................................................186
8.3.1 本地化的宏定義 ............................................................................................187
8.3.2 Strings 文件 ...................................................................................................188
8.3.3 格式字符串的本地化 ....................................................................................189
8.4 文本存儲 .................................................................................................................190
8.5 理解字體 .................................................................................................................191
8.5.1 轉換字體 ........................................................................................................192
8.5.2 羅列字體 ........................................................................................................196
8.6 顯示文本 .................................................................................................................196
8.6.1 基本繪製 ........................................................................................................196
8.6.2 布局管理器 ....................................................................................................199
8.6.3 附件(attachment) ........................................................................................202
8.6.4 文本視圖 ........................................................................................................203
8.6.5 文本控件單元 ................................................................................................204
8.7 編寫自定義文本容器 .............................................................................................208
8.8 使用多個文本視圖 .................................................................................................212
8.9 小結 .........................................................................................................................215
第三部分 Cocoa文檔
第9 章 創建文檔驅動型應用程序 .................................................................218
9.1 Cocoa 文檔模型 ......................................................................................................219
9.1.1 文件類型 ........................................................................................................220
9.1.2 文檔對象 ........................................................................................................223
9.2 創建應用程序骨架 .................................................................................................223
9.3 創建文檔 .................................................................................................................225
9.3.1 載入窗口 ........................................................................................................225
9.3.2 建立文檔模型 ................................................................................................227
9.3.3 讀入和保存文檔 ............................................................................................227
9.3.4 文檔創建事件 ................................................................................................230
9.4 擴展Outliner ...........................................................................................................231
9.4.1 定義原生文件格式 ........................................................................................231
9.4.2 定義外部文件格式 ........................................................................................233
9.4.3 支持讀入和保存 ............................................................................................236
9.5 支持撤銷 .................................................................................................................241
9.5.1 注冊撤銷操作 ................................................................................................242
9.5.2 創建撤銷組 ....................................................................................................243
9.5.3 處理撤銷操作 ................................................................................................244
9.5.4 撤銷用戶界麵 ................................................................................................244
9.6 給Outliner 增加撤銷功能 ......................................................................................245
9.7 總結 .........................................................................................................................254
第10 章 Core Data ......................................................................................255
10.1 數據建模介紹 .......................................................................................................256
10.2 理解托管對象 .......................................................................................................257
10.3 屬性類型 ...............................................................................................................258
10.4 創建數據模型 .......................................................................................................259
10.5 選擇持久存儲 .......................................................................................................267
10.5.1 內存存儲 ......................................................................................................267
10.5.2 XML 存儲 ....................................................................................................267
10.5.3 二進製存儲 ..................................................................................................268
10.5.4 SQLite 存儲 .................................................................................................268
10.5.5 自定義存儲 ..................................................................................................269
10.6 存儲元數據 ...........................................................................................................269
10.7 自動還原 ...............................................................................................................271
10.8 Core Data、Spotlight 與Time Machine ...............................................................272
10.9 小結 .......................................................................................................................273
第四部分 復雜用戶界麵
第11 章 處理結構化數據 ..............................................................................276
11.1 數據源和委托對象 ................................................................................................277
11.2 錶格 ........................................................................................................................277
11.2.1 錶格視圖的繪製 ..........................................................................................278
11.2.2 使用排序描述符 ..........................................................................................279
11.2.3 定製錶格視圖 ..............................................................................................281
11.3 大綱視圖 ................................................................................................................284
11.3.1 擴展Outliner 示例 ......................................................................................285
11.4 瀏覽器視圖 ............................................................................................................297
11.4.1 瀏覽器視圖的委托 ......................................................................................297
11.4.2 創建瀏覽器 ..................................................................................................298
11.5 集閤視圖 ................................................................................................................301
11.5.1 在集閤視圖中顯示文件 ..............................................................................302
11.6 用新控件單元定製視圖 ........................................................................................305
11.7 創建主- 從視圖 ....................................................................................................308
11.7.1 作為細節視圖的檢視器 ..............................................................................309
11.8 小結 ........................................................................................................................313
第12 章 動態視圖 ........................................................................................314
12.1 多標簽視圖 ...........................................................................................................314
12.2 分析視圖層級結構 ...............................................................................................315
12.3 修改視圖層級結構 ...............................................................................................317
12.3.1 改變視圖的父元素 ......................................................................................317
12.3.2 重排視圖 ......................................................................................................321
12.3.3 可拖齣窗口的標簽 ......................................................................................325
12.4 創建動態輸入錶單 ...............................................................................................329
12.4.1 用矩陣控件創建錶單 ..................................................................................330
12.4.2 用自定義視圖創建錶單 ..............................................................................335
12.4.3 用規則編輯器創建錶單 ..............................................................................339
12.5 全屏應用程序 .......................................................................................................343
12.6 小結 .......................................................................................................................345
第五部分 高級圖形
第13 章 自定義視圖 .....................................................................................348
13.1 圖形上下文 ...........................................................................................................349
13.2 Core Graphics ........................................................................................................350
13.3 用AppKit 繪圖 .....................................................................................................353
13.3.1 繪製形狀 ......................................................................................................354
13.3.2 用控件單元繪製 ..........................................................................................358
13.3.3 繪製文本 ......................................................................................................366
13.3.4 創建新控件單元 ..........................................................................................371
13.3.5 繪製位圖 ......................................................................................................375
13.3.6 組閤圖像 ......................................................................................................380
13.4 打印與視圖分頁 ...................................................................................................382
13.4.1 打印信息 ......................................................................................................382
13.4.2 創建打印任務 ..............................................................................................383
13.4.3 視圖分頁 ......................................................................................................385
13.4.4 自動分頁 ......................................................................................................391
13.5 用調闆擴展Interface Builder ...............................................................................392
13.6 小結 .......................................................................................................................399
第14 章 聲音與視頻 .....................................................................................400
14.1 提示音 ...................................................................................................................400
14.2 播放簡單的聲音 ...................................................................................................401
14.3 理解Cocoa 的影片對象 .......................................................................................405
14.4 加上視頻 ...............................................................................................................408
14.5 編輯媒體 ...............................................................................................................411
14.6 低層聲音API ......................................................................................................412
14.7 錄音和錄像 ...........................................................................................................414
14.8 支持語音 ...............................................................................................................417
14.9 Cocoa 語音閤成 ....................................................................................................418
14.10 跟用戶交談 .........................................................................................................419
14.11 總結 ......................................................................................................................424
第15 章 高級視覺效果 .................................................................................425
15.1 簡單的動畫 ...........................................................................................................425
15.2 Core Animation 綜述 .............................................................................................427
15.2.1 在圖層裏繪畫 ..............................................................................................428
15.2.2 設置圖層的內容 ..........................................................................................431
15.3 理解動畫的概念 ...................................................................................................432
15.4 加上簡單動畫 .......................................................................................................434
15.5 圖像濾鏡 ...............................................................................................................437
15.6 定義過渡 ...............................................................................................................443
15.7 創建復雜的動畫 ...................................................................................................445
15.8 Core Animation 三維變換 .....................................................................................446
15.9 OpenGL 與Cocoa 視圖 ........................................................................................450
15.10 Quartz 閤成器 ......................................................................................................453
15.11 總結 ......................................................................................................................454
第16 章 支持PDF 和HTML ........................................................................455
16.1 AppKit 裏的HTML...............................................................................................456
16.2 高級HTML 支持 ..................................................................................................458
16.2.1 WebKit 綜述 ................................................................................................459
16.2.2 WebView 委托 ............................................................................................460
16.2.3 編輯HTML .................................................................................................461
16.3 WebKit 的動態接口 ..............................................................................................465
16.4 PDF 和Quartz .......................................................................................................471
16.5 顯示PDF ..............................................................................................................472
16.5.1 理解PDF 標注 ............................................................................................476
16.5.2 設置文檔的元數據 ......................................................................................479
16.5.3 編輯PDF .....................................................................................................480
16.6 小結 .......................................................................................................................484
第六部分 用戶界麵集成
第17 章 搜索與過濾 .....................................................................................486
17.1 維護文檔索引 .......................................................................................................487
17.1.1 創建索引 ......................................................................................................487
17.1.2 對文檔索引 ..................................................................................................489
17.1.3 搜索一個索引 ..............................................................................................490
17.2 顯示搜索欄 ...........................................................................................................492
17.3 搜索文檔 ...............................................................................................................493
17.4 Spotlight .................................................................................................................503
17.4.1 監視文件係統事件 ......................................................................................503
17.4.2 為元數據編製索引 ......................................................................................504
17.4.3 創建元數據導入插件 ..................................................................................506
17.4.4 用Spotlight 搜索 .........................................................................................511
17.4.5 Spotlight 的局限性 ......................................................................................514
17.5 斷言 .......................................................................................................................515
17.5.1 斷言語言 ......................................................................................................515
17.5.2 直接創建斷言 ..............................................................................................517
17.5.3 使用斷言 ......................................................................................................519
17.5.4 顯示斷言編輯器 ..........................................................................................520
17.6 Quick Look ............................................................................................................523
17.6.1 預覽包 ..........................................................................................................524
17.6.2 實現Quick Look 插件 ................................................................................524
17.7 小結 .......................................................................................................................526
第18 章 聯係人、日曆和私密信息 ...............................................................528
18.1 地址簿概述 ...........................................................................................................529
18.1.1 一般概念 ......................................................................................................529
18.1.2 集閤類 ..........................................................................................................530
18.2 獲取聯係人的信息 ...............................................................................................532
18.3 檢索地址簿 ...........................................................................................................535
18.4 填寫“ 我” vCard ....................................................................................................536
18.5 在地址簿裏增加聯係人 .......................................................................................538
18.6 保存私密信息 .......................................................................................................538
18.7 日曆 .......................................................................................................................547
18.7.1 日曆存儲區的概念 ......................................................................................548
18.7.2 存取事件 ......................................................................................................549
18.7.3 新建日曆 ......................................................................................................552
18.8 同步數據 ...............................................................................................................553
18.8.1 使用Apple 的綱要 ......................................................................................554
18.9 小結 .......................................................................................................................560
第19 章 剪貼闆 ............................................................................................561
19.1 剪貼闆概覽 ...........................................................................................................561
19.2 剪貼闆類型 ...........................................................................................................564
19.3 對類型進行過濾 ...................................................................................................566
19.4 屬性列錶數據 .......................................................................................................567
19.5 自編碼對象 ...........................................................................................................567
19.6 文件和剪貼闆 .......................................................................................................568
19.7 復製和粘貼 ...........................................................................................................569
19.8 拖動和投放 ...........................................................................................................571
19.8.1 拖動操作 ......................................................................................................572
19.8.2 投放操作 ......................................................................................................574
19.8.3 在自定義視圖中實現拖放 ..........................................................................575
19.9 拖放和數據視圖 ...................................................................................................582
19.10 小結 .....................................................................................................................588
第20 章 服務 ...............................................................................................589
20.1 服務示例 ...............................................................................................................589
20.2 一個計算服務 .......................................................................................................591
20.3 使用服務 ...............................................................................................................597
20.4 控製服務菜單 .......................................................................................................599
20.5 過濾器服務 ...........................................................................................................600
20.6 小結 .......................................................................................................................601
第21 章 增加腳本支持 .................................................................................602
21.1 腳本概覽 ...............................................................................................................602
21.1.1 AppleScript 對象模型 ..................................................................................604
21.1.2 腳本詞匯錶 ..................................................................................................604
21.2 將對象腳本化 .......................................................................................................605
21.2.1 理解AppleScript 對象 ................................................................................606
21.2.2 提供對象指示符 ..........................................................................................607
21.2.3 提供一個腳本定義 ......................................................................................609
21.2.4 處理腳本命令 ..............................................................................................613
21.2.5 測試腳本支持 ..............................................................................................617
21.3 在Cocoa 程序中寫腳本 .......................................................................................620
21.4 把服務顯露給Automator .....................................................................................626
21.5 其他的腳本技術 ...................................................................................................626
21.5.1 分布式對象 ..................................................................................................627
21.5.2 F-Script .........................................................................................................627
21.6 小結 .......................................................................................................................628
第七部分 係統編程
第22 章 聯網 ...............................................................................................630
22.1 底層套接字編程 ...................................................................................................630
22.2 Cocoa 流 ................................................................................................................636
22.3 URL 的處理 ..........................................................................................................637
22.3.1 創建URL .....................................................................................................638
22.3.2 從URL 裝入 ................................................................................................638
22.3.3 擴展URL 裝載係統 ....................................................................................641
22.4 Bonjour ..................................................................................................................644
22.4.1 多播DNS .....................................................................................................644
22.4.2 DNS 服務發現 .............................................................................................645
22.4.3 瀏覽服務 ......................................................................................................645
22.4.4 通告服務 ......................................................................................................648
22.5 分布式對象 ...........................................................................................................649
22.5.1 對象的發布 ..................................................................................................650
22.5.2 獲取遠程對象 ..............................................................................................652
22.5.3 一個簡單的聊天程序 ..................................................................................653
22.6 總結 .......................................................................................................................660
第23 章 並發 ...............................................................................................661
23.1 分布式對象 ...........................................................................................................662
23.2 綫程 .......................................................................................................................664
23.2.1 創建綫程 ......................................................................................................664
23.2.2 綫程局部存儲 ..............................................................................................665
23.2.3 同步 ..............................................................................................................665
23.2.4 原子操作 ......................................................................................................673
23.2.5 未來與轉發 ..................................................................................................676
23.2.6 綫程壓力測試 ..............................................................................................678
23.3 子進程 ...................................................................................................................679
23.3.1 創建子進程 ..................................................................................................679
23.3.2 跟子進程通信 ..............................................................................................680
23.3.3 共享內存 ......................................................................................................681
23.4 操作隊列 ...............................................................................................................685
23.5 大調度中心 ...........................................................................................................690
23.6 OpenCL .................................................................................................................692
23.7 小結 .......................................................................................................................693
第八部分 附錄
第24 章 可移植的Cocoa .............................................................................696
24.1 NeXT 和Sun .........................................................................................................696
24.2 iPhone 上的移動OS X .........................................................................................697
24.3 OpenStep 和GNU .................................................................................................697
24.4 GNUstep ................................................................................................................698
24.4.1 GORM ..........................................................................................................700
24.4.2 使用GNUstep 進行構建 .............................................................................701
24.4.3 Étoilé ............................................................................................................703
24.5 QuantumSTEP .......................................................................................................703
24.6 Cocotron .................................................................................................................704
24.7 GNUstepWeb 與SOPE .........................................................................................705
第25 章 高級技巧 ........................................................................................706
25.1 預處理器 ...............................................................................................................707
25.1.1 初始化程序 ..................................................................................................707
25.1.2 For Each .......................................................................................................707
25.1.3 Objective-C 常量 .........................................................................................709
25.1.4 簡單模闆 ......................................................................................................710
25.1.5 友好的調試輸齣 ..........................................................................................711
25.2 控製結構 ...............................................................................................................711
25.2.1 NSDictionary 濫用 ......................................................................................712
25.2.2 鍵—值編碼 ..................................................................................................713
25.2.3 Trampoline ...................................................................................................714
25.2.4 狀態機 ..........................................................................................................717
25.3 整潔代碼 ...............................................................................................................719
25.3.1 Toll-Free 橋接 ..............................................................................................719
25.3.2 多重繼承 ......................................................................................................720
25.4 優化 .......................................................................................................................722
25.4.1 實例方法指針 ..............................................................................................723
25.4.2 推翻動態調度 ..............................................................................................724
25.4.3 棧中的類 ......................................................................................................726
25.4.4 對象池 ..........................................................................................................730
25.4.5 內聯函數 ......................................................................................................731
25.4.6 避免對象 ......................................................................................................733
25.5 Cocoa 跨平颱 ........................................................................................................734
25.6 運行係統 ...............................................................................................................737
25.6.1 理解對象實現 ..............................................................................................738
25.6.2 枚舉所有子類 ..............................................................................................740
25.6.3 給一個類增加方法 ......................................................................................743
· · · · · · (
收起)