3 ms·
Initializing NSArray, NSDictionary, etc. with data in it has nothing to do with ObjC as a language. No longer true as of this release. There's now direct synta
by cageface 14y ago
Initializing NSArray, NSDictionary, etc. with data in it has nothing to do with ObjC as a language.
No longer true as of this release. There's now direct syntactic support in the compiler for them.
See:
http://weblog.bignerdranch.com/398-objective-c-literals-part-1/ http://weblog.bignerdranch.com/398-objective-c-literals-part...
What you couldn't do until C++11 (and even then only in the bleeding edge compilers) is this:
std::vector<int> foo = { 1, 2, 3 };
But the gnarly template mechanics it takes to make that work will poke through in error messages and the debugger etc.
- kelnos 14y agoGah, sorry, I didn't get the feeling that that's what you were referring to, though, since it felt like you were implying that ObjC has had this feature for a long time (which it hasn't), and this is something C++ has just gotten (which it has). I'm still not convinced that the standard data structures in ObjC are now really "a part of the language", though the distinction may be meaningless and just me splitting hairs. For instance, take NSString and the @"foo" literal syntax. With GNU gcc (not sure about Apple gcc), you can pass -fconstant-string-class=X to the compiler, and actually replace NSString (well, actually NSConstantString or NXConstantString, depending on if you're using the GNU or NeXT runtime) with another class to represent string literals (I assume it has to have the same memory layout). So I wonder if they're doing something similar with NSArray and NSDictionary. Are those particular classes actually a part of the language, or is just the concept of an array/vector and dict/map a part of the language, and it can be mapped to some arbitrary class with a particular memory layout using new syntactic sugar. Also telling is that you can't initialize globals/static globals with the new array/dict syntax. That implies to me that the compiler isn't actually generating an ObjC class in rodata, but is instead taking the new syntax and transforming it (I hesitate to say "preprocessing") by emitting code for the corresponding initializer method call (or something not entirely unlike that). But I guess in the end it mostly doesn't matter how it works under the hood: as long as the syntax is there, and the compiler can generate useful warnings/errors when you make mistakes, it might as well be a part of the language. (Also, thanks for the info on C++11; I didn't realize the new STL container intializers had made it in. I'm mostly a C++-hater, but good to know nonetheless.)