2011-12-08 29 views
1

我想我缺少的東西非常基本的在這裏,但它在這裏。initWithContentsOfFile:編碼:錯誤: - NSInvalidArgumentException

在Xcode中內嵌幫助告訴我,initWithContentsOfFile:棄用

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

相反initWithContentsOfFile:編碼:錯誤:應該被使用。

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

我的問題是initWithContentsOfFile:工作正常,而下面的代碼引發錯誤。

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

錯誤:

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' 
+1

您定位的IOS版本是什麼。據我可以告訴版本2.0後的一切[[NSMutableDictionary alloc] initWithContentsOfFile:filePath]。文檔是[here](http://developer.apple.com/library/IOs/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDictionary)。我在NSDictionary的文檔中看不到initWithContentsOfFile:encoding:error,所以我會避免這樣做。 –

+0

Thaks的答覆!我對iOS 4.3非常感興趣。 – Kermit

+0

我注意到了,XCode警告是針對NSString的,儘管我使用的是NSDictionary。奇怪。再次感謝克里斯! – Kermit

回答

1

正如克里斯向我指出,NSDictionary中按預期工作。

雖然在alt-XCode中單擊方法時我仍然收到以下消息。 奇怪的是,文檔是用於NSString和NOT NSDictionary。 應該把我的閱讀護目鏡,再次感謝克里斯指出答案!

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