1

I think I am missing something very basic here, but here it goes.

The inline help in XCode tells me that initWithContentsOfFile: deprecated

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

Instead initWithContentsOfFile:encoding:error: should be used.

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding  error:NULL];

My problem is that initWithContentsOfFile: works fine while the code below raises an error.

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"prefs" ofType:@"plist"];
Load preferences into symbol dictionary
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding  error:NULL];

ERROR:

2011-12-08 16:27:12.209  -[__NSPlaceholderDictionary initWithContentsOfFile:encoding:error:]: unrecognized selector sent to instance 0x4b2a370
2011-12-08 16:27:12.212 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithContentsOfFile:encoding:error:]: unrecognized selector sent to instance 0x4b2a370'
Kermit
  • 2,865
  • 3
  • 30
  • 53
  • 1
    What version of IOS are you targeting. As far as I can tell everything after version 2.0 has [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]. Documentation is [here](http://developer.apple.com/library/IOs/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDictionary). I see no mention of initWithContentsOfFile:encoding:error in the documentation for NSDictionary, so I would steer clear of that. – Chris Knadler Dec 08 '11 at 16:03
  • Thaks for the reply! I am aming for iOS 4.3. – Kermit Dec 08 '11 at 16:29
  • I noticed now, the XCode warning was for NSString, although I am using NSDictionary. Strange. Thanks again Chris! – Kermit Dec 08 '11 at 16:45

1 Answers1

1

As Chris pointed out to me, NSDictionary is working as expected.

I still get the message below when alt-clicking the method in XCode though. Oddly enough the documentation is for NSString and NOT NSDictionary. Should have put my reading goggles on, thanks again Chris for pointing out the answer!

initWithContentsOfFile:
Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path. (Deprecated in iOS 2.0. Use initWithContentsOfFile:encoding:error: or initWithContentsOfFile:usedEncoding:error: instead.)

- (id)initWithContentsOfFile:(NSString *)path
Discussion
Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path. If the contents begin with a byte-order mark (U+FEFF or U+FFFE), interprets the contents as Unicode characters; otherwise interprets the contents as data in the default C string encoding. Returns an initialized object, which might be different from the original receiver, or nil if the file can’t be opened.

Availability
Available in iOS 4.0 and later.
Deprecated in iOS 2.0.
See Also
– initWithContentsOfFile:encoding:error:
– initWithContentsOfFile:usedEncoding:error:
Declared In
NSString.h
Kermit
  • 2,865
  • 3
  • 30
  • 53