Reputation: 58
Is there any meaningful difference between +dictionaryWithContentsOfFile:
+arrayWithContentsOfFile:
and NSPropertyListSerialization
(combined with NSData
or NSStream
), assuming that I know the file is of a specific type (e.g. I don't need to deserialize an arbitrary property list)?
Of course, I'll need to create an NSStream
or NSData
object before using NSPropertyListSerialization
, so the collection methods are a little more succinct. But if one is faster or preferable in any way, I'd rather use that.
Upvotes: 0
Views: 40
Reputation: 17040
This is easy enough to test. Start with a short sample program:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSURL *url = ...
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:url];
NSLog(@"%@", dict);
}
return 0;
}
This prints the contents of our plist file, as we would expect. Now, we put a breakpoint on the first line and run it again. Once we get the lldb prompt, we type:
(lldb) breakpoint set -r '.*NSPropertyListSerialization.*'
Then, click "Continue" to resume execution. Sure enough, we stop on a breakpoint:
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x00007fff4f22a018 Foundation`+[NSPropertyListSerialization propertyListWithData:options:format:error:]
frame #1: 0x00007fff4f229bbf Foundation`+[NSDictionary(NSDictionary) newWithContentsOf:immutable:] + 158
frame #2: 0x00007fff4f242fe9 Foundation`+[NSDictionary(NSDictionary) dictionaryWithContentsOfURL:] + 45
frame #3: 0x0000000100000e7d MyGreatProject`main(argc=1, argv=0x00007ffeefbff6a0) at main.m:15
frame #4: 0x00007fff75461015 libdyld.dylib`start + 1
So, from this, we can determine that +dictionaryWithContentsOfURL:
simply calls through to NSPropertyListSerialization
to do its dirty work. So in practice, it's reasonable to conclude that it doesn't matter which you use, although NSPropertyListSerialization
offers more flexibility through its options
parameter and error reporting.
Upvotes: 2
Reputation: 285069
There is no significant difference. NSPropertyListSerialization
is just more customizable.
For an ordinary array or dictionary there is nothing wrong to use ...withContentsOf...
Upvotes: 0