XCode中的的一些便捷用法

我也不知道該怎麼總結這些內容,就用這個名字吧,我以爲挺有用的。 轉自: http://blog.csdn.net/genios/article/details/7821133 感謝博主的整理 @synthesize by default(屬性自動綁定)在xcode4.4之前,當咱們想爲類添加一個新的屬性,通常都要對應寫實例變量和相應的synthesis,可是在Xcode 4.4以後,synthesis如今會對應property自動生成。默認行爲下,對於屬性foo,當開發者沒有寫相應的synthesis的時候,編譯器會自動在實現文件中爲開發者補全synthesis,就好像你寫了@synthesis foo = _foo。  

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

●  除非開發者在實現文件中提供getter或setter,不然將自動生成數組

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

●  只要寫了synthesis,不管有沒有跟實例變量名,都將生成實例變量.net

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

●  dynamic優先級高於synthesisorm

           ● 對於寫了@dynamic的實現,全部的對應的synthesis都將不生效blog

@literals(簡寫)ip

在xcode4.4之前ci

NSNumberelement

全部的[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;     Segues xcode 4.5的storyboard提供了更方便的segue方法。 當你要實現按cell中的箭頭實現segue時。以往都要用代碼來實現。xcode4.5中提供了直接在storyboard中連接的方法       Unwind Segues   有了Unwind segues,你能夠很容易就實現segue到你制定的一個View上。   你要在制定目標的controller中實現如下兩個方法。   -(BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender { return YES; } (默認YES)   - (IBAction)done:(UIStoryboardSegue *)segue { // React to the impending segue // Pull state back, etc. }     CollectionView   下面這幅圖就是用Collection Views實現的一個照片牆顯示。 相似於瀑布流的展現方法。     爲何要使用Collection Views呢?         ■         能夠高度定製內容的展示       ■         管理數據最佳的作法     ■ 即便是處理大量數據,也很是的高效   對於CollectionView主要要實現的方法有三個   UICollectionViewDataSource ●section的數量 -numberOfSectionsInCollection: ●某個section裏有多少個item -collectionView:numberOfItemsInSection: ●對於某個位置應該顯示什麼樣的cell -collectionView:cellForItemAtIndexPath:   embed segue 在以往的xcode中,若是咱們想要添加一個子視圖,咱們須要用代碼實現。   UIViewController *child = [[self storyboard] instantiateViewControllerWithIdentifier:@"ContentScene"]; [self addChildViewController:child]; [[self view] addSubview:[child view]]; [[child view] setFrame:frame]; 如今在storyboard多了container view這個控件,可讓你不用代碼實現添加一個子視圖。 你能夠在 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 中實現參數的傳遞。 方法順序 若是有如下代碼: @interface SongPlayer : NSObject - (void)playSong:(Song *)song; @end @implementation SongPlayer - (void)playSong:(Song *)song { NSError *error; [self startAudio:&error]; ... } - (void)startAudio:(NSError **)error { ... } @end 在早一些的編譯環境中,上面的代碼會在[self startAudio:&error]處出現一個實例方法未找到的警告。因爲編譯順序,編譯器沒法得知在-playSong:方法以後還有一個-startAudio:,所以給出警告。 在新編譯器裏,若是在同一實現文件中,不管方法寫在哪裏,編譯器均可以在對方法實現進行編譯前知道全部方法的名稱,從而避免了警告。   枚舉改進   從Xcode4.4開始,有更好的枚舉的寫法了:   typedef enum NSNumberFormatterStyle : NSUInteger { NSNumberFormatterNoStyle, NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle } NSNumberFormatterStyle;
相關文章
相關標籤/搜索