@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;
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;