Objective-C Literals
The updated Clang documentation:
Using array and dictionary literals is safer than the variadic creation forms commonly in use today. Array literal expressions expand to calls to
+[NSArray arrayWithObjects:count:]
, which validates that all objects are non-nil
. The variadic form,+[NSArray arrayWithObjects:]
usesnil
as an argument list terminator, which can lead to malformed array objects. Dictionary literals are similarly created with+[NSDictionary dictionaryWithObjects:forKeys:count:]
which validates all objects and keys, unlike+[NSDictionary dictionaryWithObjectsAndKeys:]
which also uses anil
parameter as an argument list terminator.
There are lots of carrots for going 64-bit-only and adopting the modern Objective-C runtime.
9 Comments RSS · Twitter
I don't see the issue with having nil as a terminator. You just have to know your code, as always,
For instance, it's not unusual for me to set the item before nil in arrayWithObjects: to be an object that can probably be nil itself.
So in this case, the convenience would become an inconvenience,
@bob This issue has bit me a couple times over the years, when some system class that should never have returned nil did exactly that, and so half my dictionary was wiped out—silently. I think failing fast is a good thing.
The old way still works for those cases where you expect a possible nil at the end. Although, maybe it would be a good idea to use a different method name to document your intentions.
@michael: "when some system class that should never have returned nil did exactly that, "
I usually check for nil in most cases except when I don't care if it is nil.
Nope but:
- I mostly use it to check for the existence of files. So if it's nil, fileExistsBlahBlah will return NO and the error will be handled.
- The day I start trusting NSFileManager for complex operations such as copying, renaming, getting most attributes, symbolic link has yet to come.
@bob I didn’t have much user for NSFileManager when it wasn’t threadsafe, so I wrote replacements for most of its methods. Lately, it seems pretty good except that it still doesn’t copy Finder comments.
@Michael: I never had to deal with thread-safe issue in my usage of NSFileManager as it is only invoked on the main thread. For copy operations, I still rely on the FS API that is based on the old FSCopy sample code.
Probably will have to move to NSFileManager in the not so distant future though.
@bob Hmm, I found the FS API to be crashy, and it would sometimes say that it had deleted a file without doing so. I wish NSFileManager would learn the preflighting and progress stuff, though.
[…] Objective-C literals are described as an easy sugar feature that Apple added while much of the team was busy with Swift. I have never understood why it took so long for this feature to arrive. It could have been added to Objective-C 1.x but instead came about 6 years after Objective-C 2.0. Or, to put a positive spin on this, the rate of progress with Swift is incredible compared with what we saw with Objective-C. […]