標籤:style blog color io os ar 使用 for strong
Chapter 1
Apple’s Cocoa (for OS X) 和 Cocoa Touch (for iOS) toolkits 都是用 Objective-C寫的.
Chapter 2
(1) .m 代表 “messages”的縮寫
.m -> Object-C compiler .c -> C compiler .cpp -> C++ compiler
(2)在 Objective-C中, #import 保證了每個標頭檔只被包含一次,如同c中的#ifdef標頭檔衛士的作用一樣。
(3)NSLog()如同 C中的printf(),不過增加了時間和日期戳,並自動在行尾加了“\n"分行符號。
@(" ")表示" "中的字串被當做一個NSString單元來處理。在NSLog中如果直接使用了C風格的字串”“而缺少@(),則在編譯時間得到一個warning,運行時會crash。
(4)BOOL 類型
在Objectvie-C中,BOOL類型是實際上是一個signed char的typedef,因此不僅可以用YES(值=1)和NO(值=0)表示C中的true和false,而且可以存放其他的值。這裡會有一個易錯點:如果將一個short或int等類型的值賦給BOOL變數後,會截斷末尾的8位,如果這末尾8位值剛好為0(如8960,其十六進位值為 0X2300),此值的BOOL值就是0,即NO,而不是非NO了。
Chapter 3
(1)電腦科學的核心思想:Indirection。Instead of using a value dirctly in your code, use a pointer to the value; Instead of doing something by yourself, ask another person to do it.
(2)一個檔案讀取樣本:
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){ if(argc == 1) { NSLog (@"You need to provide a file Name"); return (1). } FILE *wordFile = fopen(argv[1], "r"); // argv[0] is the code file path char word[100]; while(fgets(word, 100, wordFile) { // strip off the trailing \n word[strlen(word) - 1] = ‘\0‘; NSLog(@"%s kis %lu characters long", word, strlen(word)); } fclose(wordFile); return(0).} // main
(3)在Objective-C中,方法調用採用的是中綴標記法(infix notation),如
[circle setFillColor: kRedColor];
如果一個方法無參數,則無冒號和參數列表;如果有參數,則有冒號:
- (void) scratchTheCat; // 無參數- (void) scratchTheCat: (CatType) critter; // 有參數
Chapter 6
(1)Objective-C類的代碼可以分為2個部分:
1>. @interface 部分
@interface XX : YY{ variables;} public method declarations.@end
提供了該類的公用介面聲明。
2>. @implementation 部分
@implementation XXprivate self-used methods@end
提供了該類的實現以及內建函式。
Learn Objectvie-C on the Mac 2nd Edition 筆記