日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

xcode 4.5 new feature __ ios6 新特性 (转)

發布時間:2024/10/12 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xcode 4.5 new feature __ ios6 新特性 (转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上兩周看了wwdc 2012 developer session 400 - 412的視頻,下面總結一下xcode4.5的新特性。(部分參考onevcat的文章,在此感謝。)


@synthesize by default(屬性自動綁定在 xcode4.4以前,當我們想為類添加一個新的屬性,一般都要對應寫實例變量和相應的synthesis,但是在Xcode 4.4之后,synthesis現在會對應property自動生成。默認行為下,對于屬性foo,當開發者沒有寫相應的synthesis的時候,編譯 器會自動在實現文件中為開發者補全synthesis,就好像你寫了@synthesis foo = _foo。


總結一下,新的屬性綁定規則如下:

● ?除非開發者在實現文件中提供getter或setter,否則將自動生成

● 除非開發者同時提供getter和setter,否則將自動生成實例變量

●? 只要寫了synthesis,無論有沒有跟實例變量名,都將生成實例變量

? ? ? ? ? ?●? 如開發者寫了@synthesize foo;那么實例變量名就是foo

●? dynamic優先級高于synthesis

? ? ? ? ? ?● 對于寫了@dynamic的實現,所有的對應的synthesis都將不生效



@literals(簡寫)

在xcode4.4以前

NSNumber

所有的[NSNumber numberWith…:]方法都可以簡寫了:

●? [NSNumber numberWithChar:‘X’]簡寫為 @‘X’;

●? [NSNumber numberWithInt:12345] 簡寫為 @12345

●? [NSNumber numberWithUnsignedLong:12345ul] 簡寫為 @12345ul

● [NSNumber numberWithLongLong:12345ll] 簡寫為 @12345ll

●? [NSNumber numberWithFloat:123.45f] 簡寫為 @123.45f

●? [NSNumber numberWithDouble:123.45] 簡寫為 @123.45

●? [NSNumber numberWithBool:YES] 簡寫為 @YES

?

NSDictionary

●? [NSDictionary dictionary] 簡寫為 @{}

●? [NSDictionary dictionaryWithObject:o1forKey:k1] 簡寫為 @{ k1 : o1 }

●? [NSDictionarydictionaryWithObjectsAndKeys:o1, k1, o2, k2, o3, k3, nil] 簡寫為 @{ k1 : o1, k2 : o2, k3 : o3 }

?

?

當寫下@{ k1 : o1, k2 : o2, k3 : o3 }時,實際的代碼會是

// compiler generates:

id objects[] = { o1, o2, o3 };

id keys[] = { k1, k2, k3 };

NSUInteger count = sizeof(objects) / sizeof(id);

dict = [NSDictionary dictionaryWithObjects:objects forKeys:keyscount:count];

?

NSArray

部分NSArray方法得到了簡化:

● [NSArray array] 簡寫為 @[]

●? [NSArray arrayWithObject:a] 簡寫為 @[ a ]

●? [NSArray arrayWithObjects:a, b, c, nil] 簡寫為 @[ a, b, c ]

?

?

比如對于@[ a, b, c ],實際編譯時的代碼是

// compiler generates:

id objects[] = { a, b, c };

NSUInteger count = sizeof(objects)/ sizeof(id);

array = [NSArray arrayWithObjects:objectscount:count];


Mutable版本和靜態版本
上面所生成的版本都是不可變的,想得到可變版本的話,可以對其發送-mutableCopy消息以生成一份可變的拷貝。比如

NSMutableArray *mutablePlanets = [@[?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"Mercury", @"Venus",?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"Earth", @"Mars",?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"Jupiter", @"Saturn",?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"Uranus", @"Neptune" ]?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mutableCopy];


另外,對于標記為static的數組,不能使用簡寫為其賦值(其實原來的傳統寫法也不行)。

如果直接賦值就會提示出錯

@implementation MyClass


static NSArray *? thePlanets = @[ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?error:array literals not constant

? @"Mercury", @"Venus", @"Earth",

? @"Mars", @"Jupiter", @"Saturn",

? @"Uranus", @"Neptune"

];


解決方法是在類方法+ (void)initialize中對static進行賦值。

@implementation MyClass


static NSArray *thePlanets;?

+ (void)initialize{?

? ? if (self == [MyClass class]) {?

? ? ? ? thePlanets = @[ @"Mercury", @"Venus", @"Earth", @"Mars", @"Jupiter", @"Saturn", @"Uranus", @"Neptune" ];?

? ? }?

}


下標

Array

? ? Song *oldSong = [_songs objectAtIndex:idx];

? ? [_songs replaceObjectAtIndex:idx withObject:newSong];

可以簡寫為

? ??Song *oldSong = _songs[idx];

? ? _songs[idx] = newSong;


Dictionary

? ??id oldObject = [_storage objectForKey:key];

? ? [_storage setObject:newobject forKey:key];

可以簡寫為

? ??id oldObject = _storage[key];

? ? _storage[key] = newObject;


而且你不僅僅能使用它所提供的下標訪問。你也可以對自定義的類使用下標訪問。

對于我們自定義的類,只需要實現一下的方法就能使用下標訪問。

?

Array

- (elementType)objectAtIndexedSubscript:(indexType)idx;?

- (void)setObject:(elementType)object atIndexedSubscript:(indexType)idx;

Dictionary

- (elementType)objectForKeyedSubscript:(keyType)key;?

- (void)setObject:(elementType)object forKeyedSubscript:(keyType)key;


轉載于:https://www.cnblogs.com/51fanli/archive/2013/02/19/2916745.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的xcode 4.5 new feature __ ios6 新特性 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。