你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS6下Objective

iOS6下Objective

編輯:關於IOS

WWDC2012發布了iOS6,同時為Objective C帶來了一些新特性以簡化編程。下面是這些新特性,需要XCode4.4及以上版本支持:

1.方法的申明順序不再要求 在方法裡面可以調用在後面申明的方法,編譯器會幫助查找方法的申明,順序不再要求。如下: @interface SongPlayer : NSObject - (void)playSong:(Song *)song; @end @implementation SongPlayer - (void)playSong:(Song *)song {   NSError *error;   [self startAudio:&error];//XCode4.4以前會提示方法未定義,XCode4.4以後可以放心使用   ... } - (void)startAudio:(NSError **)error { ... } @end 2.枚舉支持強類型 XCode4.4以前定義枚舉使用如下方式,相當於定義了類型為int的枚舉類型。 typedef enum {     NSNumberFormatterNoStyle,     NSNumberFormatterDecimalStyle,     NSNumberFormatterCurrencyStyle,     NSNumberFormatterPercentStyle,     NSNumberFormatterScientificStyle,     NSNumberFormatterSpellOutStyle } NSNumberFormatterStyle; // typedef int NSNumberFormatterStyle; XCode4.4以後可以為枚舉指明強類型,這樣在賦值時會有強類型限制(需要在Build Setting開啟Suspicious implicit conversions)。定義如下: typedef enum NSNumberFormatterStyle : NSUInteger {     NSNumberFormatterNoStyle,     NSNumberFormatterDecimalStyle,     NSNumberFormatterCurrencyStyle,     NSNumberFormatterPercentStyle,     NSNumberFormatterScientificStyle,     NSNumberFormatterSpellOutStyle } NSNumberFormatterStyle; 或使用NS_ENUM宏來定義 typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {     NSNumberFormatterNoStyle,     NSNumberFormatterDecimalStyle,     NSNumberFormatterCurrencyStyle,     NSNumberFormatterPercentStyle,     NSNumberFormatterScientificStyle,     NSNumberFormatterSpellOutStyle }; 3.默認屬性合成 @interface Person : NSObject @property(strong) NSString *name; @end @implementation Person {     NSString *_name;//這句可以省略,XCode很早就可以了 } @synthesize name = _name;//XCode4.4以後,這句也可以省略,XCode默認合成帶下劃線的成員變量 @end 即可以簡化為: @interface Person : NSObject @property(strong) NSString *name;//ARC開啟,否則需要自己release @end @implementation Person @end 4.創建NSNumber的新語法 XCode4.4以前的創建方式: NSNumber *value; value = [NSNumber numberWithChar:'X']; value = [NSNumber numberWithInt:12345]; value = [NSNumber numberWithUnsignedLong:12345ul]; value = [NSNumber numberWithLongLong:12345ll]; value = [NSNumber numberWithFloat:123.45f]; value = [NSNumber numberWithDouble:123.45]; value = [NSNumber numberWithBool:YES]; XCode4.4以後可簡化為: NSNumber *value; value = @'X'; value = @12345; value = @12345ul; value = @12345ll; value = @123.45f; value = @123.45; value = @YES; XCode4.4以前,使用語句創建NSNumber: NSNumber *piOverSixteen = [NSNumber numberWithDouble: ( M_PI / 16 )]; NSNumber *hexDigit = [NSNumber numberWithChar: "012345679ABCDEF"[i % 16]); NSNumber *usesScreenFonts = [NSNumber numberWithBool:                               [NSLayoutManager usesScreenFonts]]; NSNumber *writingDirection = [NSNumber numberWithInt:                                 NSWritingDirectionLeftToRight]; NSString *path = [NSString stringWithUTF8String: getenv("PATH")]; XCode4.4以後可以通過”()”方式創建: NSNumber *piOverSixteen = @( M_PI / 16 ); NSNumber *hexDigit = @( "012345679ABCDEF"[i % 16] ); NSNumber *usesScreenFonts = @( [NSLayoutManager usesScreenFonts] ); NSNumber *writingDirection = @( NSWritingDirectionLeftToRight ); NSString *path = @( getenv("PATH") ); 5.創建NSArray的新語法 NSArray* array; array = @[ a, b, c ]; //相當於使用下面的方式創建: id objects[] = { a, b, c }; NSUInteger count = sizeof(objects)/ sizeof(id); array = [NSArray arrayWithObjects:objects count:count]; 6.創建NSDictionary的新語法 NSDictionary *dict; dict = @{}; dict = @{ k1 : o1 }; dict = @{ k1 : o1, k2 : o2, k3 : o3 }; //相當於如下方式: id objects[] = { o1, o2, o3 }; id keys[] = { k1, k2, k3 }; NSUInteger count = sizeof(objects) / sizeof(id); dict = [NSDictionary dictionaryWithObjects:objects                                    forKeys:keys                                      count:count]; 7.mutable對象的創建,調用對象的-mutableCopy方法 NSMutableArray *mutablePlanets = [@[   @"Mercury", @"Venus", @"Earth",   @"Mars", @"Jupiter", @"Saturn",   @"Uranus", @"Neptune" ] mutableCopy]; 8.靜態容器對象的創建,使用+initialize方法 @implementation MyClass static NSArray *thePlanets; + (void)initialize {   if (self == [MyClass class]) {     thePlanets = @[       @"Mercury", @"Venus", @"Earth",       @"Mars", @"Jupiter", @"Saturn",       @"Uranus", @"Neptune"     ];   } } 9.可變數組新的存取方式: @implementation SongList {   NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx {     Song *oldSong = _songs[idx];//使用[idx]訪問子對象     _songs[idx] = newSong;//使用[idx]設置子對象     return oldSong ;www.2cto.com } 10.可變字典新的存取方式: @implementation Database {   NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id )key {     id oldObject = _storage[key];//相當於id oldObject = [_storage objectForKey:key];     _storage[key] = newObject;//相當於[_storage setObject:object forKey:key];     return oldObject; }
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved