`NSString`, `NSDictionary`, `NSArray`, 和 `NSNumber` 字面值應該用在任何創建不可變的實例對象。特別小心 `nil` 不能放進 `NSArray` 和 `NSDictionary` 里,這會導致 Crash。
**例子:**
~~~
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone" : @"Kate", @"iPad" : @"Kamal", @"Mobile Web" : @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingZIPCode = @10018;
~~~
**不要這樣做:**
~~~
NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingZIPCode = [NSNumber numberWithInteger:10018];
~~~
對于那些可變的副本,我們推薦使用明確的如 `NSMutableArray`, `NSMutableString` 這些類。
下面的例子 **應該被避免**:
~~~
NSMutableArray *aMutableArray = [@[] mutableCopy];
~~~
上面的書寫方式存在效率以及可讀性的問題。效率方面,一個不必要的不可變變量被創建,并且馬上被廢棄了;這并不會讓你的 App 變得更慢(除非這個方法會被很頻繁地調用),但是確實沒必要為了少打幾個字而這樣做。對于可讀性來說,存在兩個問題:第一個是當瀏覽代碼并且看見 `@[]` 的時候你的腦海里馬上會聯系到 `NSArray` 的實例,但是在這種情形下你需要停下來思考下。另一個方面,一些新手看到后可能會對可變和不可變對象的分歧感到不舒服。他/她可能對創造一個可變對象的副本不是很熟悉(當然這并不是說這個知識不重要)。當然,這并不是說存在絕對的錯誤,只是可用性(包括可讀性)有一些問題。