使用協議委託任務協議是一個聲明某些方法及屬性並儲存在實體文檔。協議就像是一些規範,實踐協議的類必須遵守這些規範。建立協議 xcode-->File -->New --> New File --> Cocoa Touch --> Objective - C Protocol -->Next -->協議名稱(eg:PersonProtocol)-->Save實際聲明協議:#import<Foundation/Foundation.h>@protocol PersonProtocol<NSObject>@optional@property(nonamotic,strong)NSString *firstName;@property(nonamotic,strong)NSString *lastName;@required //必須實現的方法和屬性@property(nonamotic,unsafe_unretatined)NSUInteger arg;-(void)breathe;@end建立一個Father類,實現協議#import<Foundation/Foundation.h>#import "PersonProtocol.h"@interface Father:NSObject<PersonProtocol>-(void)breathe;@end //.m裡面要有執行個體方法。協議裡的關鍵字 @required 明確指定必須的屬性與方法。@optional 可以實踐或不需要實踐使用監測執行個體方法或類方法是否有效使用環境:你開發的SDK是最新版的,但是你希望支援執行舊版IOS SDK的裝置。使用 NSObject 的 instancesRespondToSelector:類方法檢測指定的 selector 是否存在類執行個體中。要確認一個類 是否響應本身的類方法,需使用 respondsToSelector:類方法。你可以使用同樣方式檢測,在一個執行個體的執行個體方 法,透過 instancesRespondToSelector:NSObject 的類方法 這邊有兩個關於 iOS SDK 的重要概念要記住:Base SDK(基底 SDK)這個 SDK 是用來編譯應用程式。可能是最新最大的 SDK,且能存取所有新的 API。Deployment SDK/Target(部署 SDK)這邊的 SDK 使指定你希望編譯後並執行的裝置 SDK 版本。因為就事實上,編譯應用是基於 Base SDK 與 Depolyment SDK 兩個 SDK 編譯。 舉個例子:NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Item 1",@"Item 4",@"Item 2", @"Item 5",@"Item 3", nil];NSLog(@"Array = %@", array);if ([NSArray instancesRespondToSelector:@selector(sortUsingComparator:)]){ /* 使用 sortUsingComparator: 執行個體方法來排列數組*/ }else if ([NSArray instancesRespondToSelector: @selector(sortUsingFunction:context:)]){ /* 使用 sortUsingFunction:context: 實體方法做數組排序*/ }else {/* 處理其他狀況 */ } 確認類是否可在運行期中使用使用環境:你正在使用最新版的 SDK,但是你無法確定是否可以使用,因為無法肯定使用者是否有安裝最新版的 SDK。 使用字串使用NSString 和NSMutableString 類NSString 類不能更改的,NSString類一旦被建立,內容就不能修改了。可變字串NSMutableString建立以後還可以修改。NSString *string = @"hello world";NSString *string = [NSString stringWithString:@"hello world"];NSMutableString同上字串長度 [string length];字串類型轉換:NSString *string = @"123.456";NSInteger integerOfString = [string integerValue]; //123CGFloat floatOfString = [string floatValue]; //123.456001Double doubleOfString = [string doubleValue]; //123.456.000字串中尋找字串NSString *one = @"hello world";NSString *two = @"hello";NSRange range = [one rangeOfString:two];if(range.location == NSNotFound){ /*Coule NOT find nedle in haystack*/}else{ NSLog(@"%@",range.location);}數字NSNumber 類用來物件導向的方法處理數字。NSInteger 有符號數(正數或負數) NSUInteger無符號數(正數和0)CGFloat 和 double 操作浮點數。numberWithInteger:將一個整型值封裝成一個NSNumber執行個體numberWithUnsignedInteger:講一個無符號整型值封裝成一個NSNumber執行個體numberWithFloat:將一個浮點數封裝成一個NSNumber執行個體numberWithDouble:將一個double類型的數封裝成一個NSNumber執行個體下面這些方法用來從一個NSNumber執行個體中提取純數字:integerValue/unsignedIntegerValue/floatValue/doubleValue數字和一個字串比較方法:NSNumber *unsighedNumber = [NSNumber numberWithUnsignedInteger:123456];NSString *numberInString = [NSString stringWithFormat:@"%lu",(unsigned long)[unsignedNumber unsignedIntegerValue]];NSLog(@"%@",numberInString); 分配和使用數組:NSArray 和 NSMutableArray 類NSArray *array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",nil];自動釋放的數組:NSArray*array = [NSArray arrayWithObjects: @"one",@"two",@"three",nil];數組長度 [array count];nsmutableArray數組增加對象[array addObject:@"four"];刪除數組[array removeObject:@"four"];擷取數組指定位置的對象objectAtIndex:分配和使用 Dictionary分配和使用SetsSets和array非常相似,二者最大的區別就是sets中相同對象只能被添加一次。當你第二次添加同一個對象時,sets會拒絕添加。addObject 增加對象 removeObject 刪除對象快速遍曆一個set中所有的對象 enumerateObjectsUsingBlock:方法。例如[setOfNames enumerateObjectsUsingBlock:^(__strong id obj,BOOL *stop){ if([obj isKindOfClass:[NSString class]]){ NSString *string = (NSString *)obj; if([string isEqualToString:@"Kiyosaki"){ NSLog(@"Found %@ in the set",string); *stop = YES; } }} 通過NSNotificationCenter發送通知通過App發布一條通知同時允許其他對象接收通知並採取行動,這取決於你發布的通知。使用NSNotificationCenter中default notificationcenter的postNotificationName:object:userInfo:的執行個體方法發布一條通知,其中攜帶一個對象(通常此對象啟用通知)和一條使用者資訊詞典,詞典中包含了關於詞條通知或者啟用通知的對象的額外資訊。