2012-08-03 64 views
2
這種有用的創造者

我今天發現,有一個更乏味的方式來動態創建數組和字典:從什麼時候開始,我們擁有的NSArray和NSDictionary的

NSArray *myArray = @[@"one", @"two", @"three"]; 

NSDictionary *myDict = @{@"key": @"value", @"key2": @"value2"}; 

這是一個相當新的除了Objective-C語言,這些結構的名稱是什麼?

回答

4

Xilin 4.4以前的新產品。我聽說他們被稱爲集合文字。

編輯補充:

您也可以參考的NSArray和NSDictionary的成員爲array[1]dictionary[@"key"]。創建語法是向後兼容的,因爲它在構建時完全展開。訪問器的語法不是因爲它涉及到運行時的更改。

+0

你現在只能通過擴展運行時的功能來完成在Lion和iOS 5上的引用,請參閱http://stackoverflow.com/a/11694878/96716 – 2012-08-03 23:56:45

2

它們是Apple的LLVM 4.0編譯器的一部分,該編譯器在XCode 4.4中發佈。

有關LLVM網站上「語法添加」部分的更多信息,請參閱here。其他新的字面語法也存在,這也在網站上記錄。

2

葉..還有一些other examples

// character literals. 
    NSNumber *theLetterZ = @'Z';   // equivalent to [NSNumber numberWithChar:'Z'] 

    // integral literals. 
    NSNumber *fortyTwo = @42;    // equivalent to [NSNumber numberWithInt:42] 
    NSNumber *fortyTwoUnsigned = @42U; // equivalent to [NSNumber numberWithUnsignedInt:42U] 
    NSNumber *fortyTwoLong = @42L;  // equivalent to [NSNumber numberWithLong:42L] 
    NSNumber *fortyTwoLongLong = @42LL; // equivalent to [NSNumber numberWithLongLong:42LL] 

    // floating point literals. 
    NSNumber *piFloat = @3.141592654F; // equivalent to [NSNumber numberWithFloat:3.141592654F] 
    NSNumber *piDouble = @3.1415926535; // equivalent to [NSNumber numberWithDouble:3.1415926535] 

    // BOOL literals. 
    NSNumber *yesNumber = @YES;   // equivalent to [NSNumber numberWithBool:YES] 
    NSNumber *noNumber = @NO;    // equivalent to [NSNumber numberWithBool:NO] 

我第一次看到它基於Java!有點酷:)

相關問題