objective-c-literals

Accessing keys in NSDictionary using [key] notation?

拥有回忆 提交于 2019-12-04 05:46:57
I have just realised that I can access NSDictionary using both objectForKey: and dict[key]? NSDictionary *coordsDict = @{@"xpos": @5.0, @"ypos": @7.2, @"zpos": @15.7}; NSLog(@"XPOS: %@", coordsDict[@"xpos"]); NSLog(@"XPOS: %@", [coordsDict objectForKey:@"xpos"]); Can anyone tell me if this has been hiding from me all along or if its some fairly recent change to the language? EDIT: The question does not generically refer to the new string literals, but more specifically to accessing NSDictionary with the same string literal syntax you would use for NSArray. I obviously overlooked this and just

Is there a literal syntax for mutable collections?

*爱你&永不变心* 提交于 2019-12-03 08:04:25
问题 I know I can create an NSArray with @[@"foo", @"bar"] or an NSDictionary with @{@0 : @"foo", @1 : @"bar"} . Is there a literal syntax for creating an NSMutableArray or an NSMutableDictionary ? 回答1: No. Just as how there isn't a syntax for creating an NSMutableString either. Mutable objects are not particularly suited to literal values. 回答2: There isn't a built in way, but I just usually use mutableCopy like this: NSMutableArray *array = [@[ @"1", @"2", @"3" ] mutableCopy]; 回答3: But, is there

What does a square-bracketed index after an NSArray mean? [duplicate]

半腔热情 提交于 2019-12-02 12:23:02
问题 This question already has answers here : What are the details of “Objective-C Literals” mentioned in the Xcode 4.4 release notes? (3 answers) Closed 5 years ago . Going through iTunes U Developing iOS 7 Apps for iPhone and iPad and in Lecture 3 slides, on page 120, there's a Quiz question that asks what the following line of code does. Frankly, I'm a bit stumped, and hoped someone could break it down. cardA.contents = @[cardB.contents,cardC.contents][[cardB match:@[cardC]] ? 1 : 0]; So, I get

Difference between literals and class methods for NSMutableArray and NSMutableDictionary [duplicate]

孤者浪人 提交于 2019-12-02 09:49:26
问题 This question already has an answer here : Is literal creation of an NSMutableDictionary less efficient than the class helper method? (1 answer) Closed 6 years ago . When I started with OSX/iOS I used NSMutableArray * a1 = [NSMutableArray arrayWithCapacity:123] ; NSMutableDictionary * d1 = [NSMutableDictionary dictionaryWithCapacity:123] ; Then I discovered the 'simpler' version NSMutableArray * a2 = [NSMutableArray array] ; NSMutableDictionary * d2 = [NSMutableDictionary dictionary] ; I have

What does a square-bracketed index after an NSArray mean? [duplicate]

梦想的初衷 提交于 2019-12-02 04:50:49
This question already has an answer here: What are the details of “Objective-C Literals” mentioned in the Xcode 4.4 release notes? 3 answers Going through iTunes U Developing iOS 7 Apps for iPhone and iPad and in Lecture 3 slides, on page 120, there's a Quiz question that asks what the following line of code does. Frankly, I'm a bit stumped, and hoped someone could break it down. cardA.contents = @[cardB.contents,cardC.contents][[cardB match:@[cardC]] ? 1 : 0]; So, I get the first part, cardA.contents = a new array with cardB.contents and cardC.contents in the array. But, next comes (I guess??

Is literal creation of an NSMutableDictionary less efficient than the class helper method?

两盒软妹~` 提交于 2019-11-29 13:14:35
Is it appreciably more efficient to create an NSMutableDictionary using a constructor [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object1", @"key1", @2, @"key2", nil]; than taking the mutable copy of a literal [@{@"key1":@"object1", @"key2":@2} mutableCopy]; As Jeremy said; measure, measure, measure.... then optimize if you have a real problem. Without looking at the source, I can think of possible reasons why either might be faster and that answer may change depending on # of entries. The real question, though, is why do you have an app architecture where it matters? It is quite rare

Should I prefer to use literal syntax or constructors for creating dictionaries and arrays?

三世轮回 提交于 2019-11-29 08:11:05
I am reading through the iOS Developer Guide to get familiarized with the Objective-C language and currently I am having a little confusion on the topic of Container Literals and Subscript Notation as it pertains to creating objects like NSDictionary . I understand that there are several ways to create NSDictionary objects including Key-Value encoding ( dictionaryWithObjects:forKeys: and dictionaryWithObjectsAndKeys: , or their corresponding initializers). Source Link. From my understanding there are two main ways to do this and then there is another way which is by using container literals,

Is literal creation of an NSMutableDictionary less efficient than the class helper method?

泪湿孤枕 提交于 2019-11-28 07:10:20
问题 Is it appreciably more efficient to create an NSMutableDictionary using a constructor [NSMutableDictionary dictionaryWithObjectsAndKeys:@"object1", @"key1", @2, @"key2", nil]; than taking the mutable copy of a literal [@{@"key1":@"object1", @"key2":@2} mutableCopy]; 回答1: As Jeremy said; measure, measure, measure.... then optimize if you have a real problem. Without looking at the source, I can think of possible reasons why either might be faster and that answer may change depending on # of

Is there any way to get the neat Objective-C literal indexing feature in Xcode 4.4?

☆樱花仙子☆ 提交于 2019-11-28 06:38:29
I read all about the new Objective-C literals, and used Xcode to convert my old code, but the indexing code didn't change. I changed it by hand but then it wouldn't compile. I saw a post that said we have to wait until iOS 6, but I want the indexing NOW! Is there any solution? David H Well, there is a way to do it! Add the indexing methods as a category to NSArray and NSDictionary, and you can get the feature for most of the classes you'd want it for. You can read up on ObjectiveC literals here . And thanks to James Webster's solution for @YES and @NO you can use them properly in your projects

Should I prefer to use literal syntax or constructors for creating dictionaries and arrays?

…衆ロ難τιáo~ 提交于 2019-11-28 02:10:40
问题 I am reading through the iOS Developer Guide to get familiarized with the Objective-C language and currently I am having a little confusion on the topic of Container Literals and Subscript Notation as it pertains to creating objects like NSDictionary . I understand that there are several ways to create NSDictionary objects including Key-Value encoding ( dictionaryWithObjects:forKeys: and dictionaryWithObjectsAndKeys: , or their corresponding initializers). Source Link. From my understanding