斯坦福大學iOS應用開發教程學習筆記(第三課)Objective-C

第二課名稱是:Objective-C 

回顧上節課的內容:

  1. 創建了單個MVC模式的項目
  2. 顯示項目的各個文件,顯示或隱藏導航,Assistant Editor, Console, Object Library, Inspector等功能的使用
  3. 在故事版上編輯視圖,通過Ctrl+拖拽把view連接到Controller的outlet。
  4. 創建新的類,比如 CalculatorBrain
  5. 使用@synthesize
  6. 延遲實例化實現getter
  7. [ ]中括號的使用
  8. 私有方法在.m文件中定義
  9. 使用strong weak屬性
  10. 處理代碼中的警告和錯誤
  11. 相關Obj-c的語法知識,比如NSString 的使用
這節課主要是講Obj-C語法,實例化初始化,內省,Foundation框架裏的主要的一些類的使用

1、爲什麼用property,理由有兩個:

  •  實體變量的安全性和繼承能力
  •  提供延遲實例化,比如:UI更新,一次性檢測。
property可以沒有實體變量,怎麼做到的呢?
不要用@synthesize,自己創建getter 和setter.
反過來,也可以有實體變量,沒有property。不過建議使用property。

2、爲什麼用.號

  •   美觀,可讀性增強
  •   可以和C語言的結構體配合
注意類型需要大寫,這是個規範。
3、strong VS  weak
strong,weak都是指針的屬性,
strong 是隻要指向那塊內存,就不能釋放。
weak  是內存有strong指向的時候才被保留,沒strong的指向weak也會被置爲nil。
weak在iOS 5才能使用。
這是引用計數技術,不是垃圾回收。當失去所有的strong的指向時,立馬釋放內存。strong weak是針對property的,本地變量都是strong的。

4、 nil =0.

給nil發送消息,也是ok的。
BOOL 類型:YES  NO;
不能用小寫的bool。

5、類方法和實例方法

+號是類方法
-號是實例方法
方法的參數識別:帶星號的,是類指針變量,內容在堆上,不帶星號的是變量在棧上。

6、實例化

通過其他對象創建對象。
通過alloc 和init創建對象
alloc是NSObject的類方法,alloc是不夠的,還要初始化,必須要初始化。
可以自定義很多的init方法,NSString 有實際中init方法比如:
- (id)initWithCharacters:(const unichar *)characters length:(int)length; 
- (id)initWithFormat:(NSString *)format, ...;
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
初始化時,要先調用父類的初始化方法。
要指定初始化方法。防治循環調用。
數組的指針都是strong 
初始化方法:
  1. @implementation MyObject  
  2. - (id)init  
  3. {  
  4.    self = [super init]; // call our super’s designated initializer   
  5.    if (self) {  
  6.    // initialize our subclass here  
  7.    }  
  8.    return self;  
  9. }   
  10. @end  
爲什麼要給self賦值呢?因爲這是一種協議機制,確保super的初始化在我們之前初始化,如果super初始化失敗,那就返回nil。
id 不等於(void*),id是obj-c的一個內置的類型。

7、動態綁定

id類型和NSString*類型實質上沒有什麼區別,實質爲了更好的找出語法方面的bug.在運行時發現消息都會去潯找消息的執行。
例子代碼:
  1. @interface Vehicle  
  2. - (void)move;  
  3. @end  
  4. @interface Ship : Vehicle  
  5. - (void)shoot;  
  6. @end  
  7. Ship *s = [[Ship alloc] init];  
  8. [s shoot];  
  9. [s move];  
  10. Vehicle *v = s;  
  11. [v shoot];  
當調用給v 發送shoot的消息時,雖然Vehicle沒有shoot方法,但是程序不會崩潰,編譯器會給個警告而已,運行時會找到v其實時有shoot方法的。
 

8、內省

id可以讓數組裏存入各種類型的對象。
如何知道id的類呢?
isKindOfClass: returns whether an object is that kind of class (inheritance included) 
isMemberOfClass: returns whether an object is that kind of class (no inheritance) 
respondsToSelector: returns whether an object responds to a given method

SEL類型
  1. SEL shootSelector = @selector(shoot);  
  2. SEL shootAtSelector = @selector(shootAt:);  
  3. SEL moveToSelector = @selector(moveTo:withPenColor:);  

[obj performSelector:shootSelector]; 無參數的SEL
[obj performSelector:shootAtSelector withObject:coordinate];有一個參數的SEL。

9、foundation 框架

NSObject的方法
-(NSString*)description ,用在NSLog,%@。
NSString對象
NSString 對Unicode編碼的任意語言的字符串,可以容納任何語言。用@""編譯成NSString 
NSString是不可變的。會返回新的字符串。NSString的使用方法太多了,建議查看文檔使用。
NSString已經優化的性能非常的好了,最好不要使用MutableString。

NSNumber 封裝原始數據比如 Int float  double等。
NSValue 封裝非對象的數據
NSData 二進制
NSDate  日曆
NSArray 有序的對象集合,不可變。下面是最常用的數組的方法。
  1. + (id)arrayWithObjects:(id)firstObject, ...; // nil-terminated arguments  
  2. NSArray *primaryColors = [NSArray arrayWithObjects:@“red”, @“yellow”, @“blue”, nil];  
  3.  + (id)arrayWithObject:(id)soleObjectInTheArray; // more useful than you might think!  
  4. - (int)count;  
  5. - (id)objectAtIndex:(int)index;  
  6. - (id)lastObject; // returns nil (doesn’t crash) if there are no objects in the array  
  7. - (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;  
  8. - (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)selectorArgument;  
  9. - (NSString *)componentsJoinedByString:(NSString *)separator;  
  10. - (BOOL)containsObject:(id)anObject; // could be slow, think about NSOrderedSet  
不能把nil放到數組中。NSNull都能放進去,但是它只是個佔位符。
copy,可變數組返回不可變
             不可變數組可以返回可變的。
NSMutableArray
  1. + (id)arrayWithCapacity:(int)initialSpace; // initialSpace is a performance hint only + (id)array;  
  2. - (void)addObject:(id)anObject; // at the end of the array - (void)insertObject:(id)anObject atIndex:(int)index;  
  3. - (void)removeObjectAtIndex:(int)index;  
  4. - (void)removeLastObject;  
  5. - (id)copy;   
可變繼承了不可變。

NSDictionary類
  1. + (id)dictionaryWithObjects:(NSArray *)values forKeys:(NSArray *)keys;  
  2. + (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;  
  3. NSDictionary *base = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:2], @“binary”,  
  4. [NSNumber numberWithInt:16], @“hexadecimal”, nil];  
  5. - (int)count;  
  6. - (id)objectForKey:(id)key;  
  7. - (NSArray *)allKeys;  
  8. - (NSArray *)allValues;  
NSMutableDicationary
  1. + (id)dictionary; // creates an empty dictionary (don’t forget it inherits + methods from super)  
  2. - (void)setObject:(id)anObject forKey:(id)key;  
  3. - (void)removeObjectForKey:(id)key;  
  4. - (void)removeAllObjects;  
  5. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;  

NSSet 不可變無序的唯一對象集合
  1. + (id)setWithObjects:(id)firstObject, ...;  
  2. + (id)setWithArray:(NSArray *)anArray;  
  3. - (int)count;  
  4. - (BOOL)containsObject:(id)anObject;  
  5. - (id)anyObject;  
  6. - (void)makeObjectsPerformSelector:(SEL)aSelector;  
NSMutableSet
  1. - (void)addObject:(id)anObject; // does nothing if object that isEqual:anObject is already in - (void)removeObject:(id)anObject;  
  2. - (void)unionSet:(NSSet *)otherSet;  
  3. - (void)minusSet:(NSSet *)otherSet;  
  4. - (void)intersectSet:(NSSet *)otherSet;  

NSOrderSet。是NSArray和NSSet的合體,比NSSet快。
  1. - (int)indexOfObject:(id)anObject;  
  2. - (id)objectAtIndex:(int)anIndex;  
  3. - (id)firstObject; and - (id)lastObject; - (NSArray *)array;  
  4. - (NSSet *)set;  
NSMutableOrderSet
  1. - (void)insertObject:(id)anObject atIndex:(int)anIndex;  
  2. - (void)removeObject:(id)anObject;  
  3. - (void)setObject:(id)anObject atIndex:(int)anIndex;  

Enumeration
  1. NSSet *mySet = ...;  
  2. for (id obj in mySet) {  
  3. if ([obj isKindOfClass:[NSString class]]) {  
  4. }  

  1. NSDictionary *myDictionary = ...;  
  2.    for (id key in myDictionary) {  
  3.     // do something with key here  
  4.     id value = [myDictionary objectForKey:key];  
  5. // do something with value here   
  6.    }  

10、property List

NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData 這6中是property List
11、NSUserDefaults是輕量級的property List存儲。
通過standardUserDefaults方法來存取。

常用方法
  1. - (void)setDouble:(double)aDouble forKey:(NSString *)key;  
  2. - (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int   
  1. - (void)setObject:(id)obj forKey:(NSString *)key; // obj must be a Property List  
  2. - (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not   
[[NSUserDefaults standardUserDefaults] synchronize];方法來同步到去存儲,任何操作後都要存儲一下,開銷不大。

不得不說,課程的信息量很大。

容芳志 (http://blog.csdn.net/totogo2010)

本文遵循“署名-非商業用途-保持一致”創作公用協議


原文地址:http://blog.csdn.net/totogo2010/article/details/8213958

發佈了27 篇原創文章 · 獲贊 3 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章