Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by __david__ on Sunday February 19, 2012 @06:00PM
(#3610684)
Because presumably (I don't have the new XCode to check) you can do things like:
[someObject dictArg:@{ a:@"B" } arrayArg:@[1,2,34]];
So if they were both using "{" to set them off the parser doesn't know which one you mean until it hits the first ":" (and it can't guess from the type when the type is "id"). Now that's not impossible to write a parser that looks ahead to figure out what you mean, but it's much easier to parse if they have completely different syntax from the outset. And the "{" vs "[" distinction is very standard amongst the higher level languages, even if it isn't in straight C.
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by bebop on Sunday February 19, 2012 @03:59PM
(#3610387)
I think they chose the [] for arrays and {} for dictionaries as that is the style that seems to be most popular right now. Python for instance uses this.
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by openbear on Sunday February 19, 2012 @11:56PM
(#3611351)
When adding features to something, I tend to be a big fan of "when in Rome"[1]. If these were extensions to Python (or Ruby or JavaScript) then I would totally be for using '[' with arrays. As a C guy though, I would rather have seen them use '{' for NSArray and '[' for NSDictionary. Regardless, I'm glad to see this stuff added.
[1] http://en.wiktionary.org/wiki/when_in_Rome,_do_as_the_Romans...
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by xsmasher on Monday February 20, 2012 @02:10AM
(#3611617)
It's already custom in objc to use {} for dicts and [] for arrays when printing them out, unless I'm misremembering. It also matches the use of those characters in JSON, so it seems natural to me.
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by __david__ on Sunday February 19, 2012 @05:50PM
(#3610665)
And Perl. And Javascript. And Ruby. There's lots of precedent.
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by icodestuff on Sunday February 19, 2012 @05:23PM
(#3610613)
Could be in case they want to do literal NSValues later. Since {}-syntax is used in C for both array literals and struct literals, they needed to change one, and the picked the more common one. Otherwise is foo in:
id foo = @{ @2, @5 };
an NSArray containing two NSNumbers, or an NSValue containing a struct with two pointers (which happen to be NSNumber *s)?
I'd guess there were other edge cases they didn't want to deal with, so they didn't do literal NSValues for this release.
Or it could be that it's because []-style arrays are popular these days.
Or some of both; in picking which {} to change to @[], they reasoned that lots of people have experience with []-style arrays.
Re:Objective-C literals for NSDictionary, NSArray, and NSNumber
by ary on Sunday February 19, 2012 @02:07PM
(#3610057)
> Why did they choose '[' over '{'? Oddly enough, NSDictionary uses '{'.
Who knows, but it seems to mirror Python in that respect. Being a fan of Python I can only approve.
When I want an array literal in C, I'd write ...
... but in Objective-C, for an NSArray literal I'd write ... Why did they choose '[' over '{'? Oddly enough, NSDictionary uses '{'.I feel like the syntax for an NSString literal is more natural (as a C developer).