0
我收到錯誤,當我試圖解析一個JSON,我不明白問題...我想從我的價值具有「objectForKey」的XML。- [__ NSCFString objectForKey:]:無法識別的選擇器發送到實例問題
我的JSON分析器:
@implementation contents
NSDictionary* loadedContent = nil;
+ (NSDictionary*)getContent:(NSString *)nameInnov
{
if (loadedContent == nil) {
NSError* error = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"res/contents.json" ofType:nil inDirectory:nil];
loadedContent = [NSJSONSerialization JSONObjectWithData:[[NSFileManager defaultManager] contentsAtPath:path] options:kNilOptions error:&error];
if (error) {
NSLog(@"Error while parsing: %@", [error localizedDescription]);
}
}
for (NSString* place in [loadedContent allKeys]) {
NSDictionary* contents = [loadedContent objectForKey:place];
for (NSString* key in [contents allKeys]) {
NSDictionary* info = [contents objectForKey:key];
if ([[info objectForKey:@"innov"] isEqualToString:nameInnov] == YES) {
return info;
}
}
}
return nil;
}
- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName attributes:(NSDictionary*)attributeDict
{
}
我打電話這個解析器在我的觀點中的一種:
- (void)viewDidLoad
{
[super viewDidLoad];
[LoginModel setOFFLandingPage];
NSDictionary* contentView = [contents getContent:@"wonderbra"];
//self.nameInnovTextField.text = [info objectForKey:@"titre"];
}
,我得到這個錯誤:
2014-02-17 15:56:45.206 App[651:60b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1559a450
2014-02-17 15:56:45.208 App[651:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1559a450'
*** First throw call stack:
(0x2edc3f4b 0x396656af 0x2edc78e7 0x2edc61cb 0x2ed154d8 0xbf945 0xb6fab 0x3153b37b 0x315e60f1 0x315e6007 0x315e55e3 0x315e530d 0x315e507d 0x315e5015 0x31536da3 0x311bdc6b 0x311b947b 0x311b930d 0x311b8d1f 0x311b8b2f 0x311b285d 0x2ed8f1cd 0x2ed8cb71 0x2ed8ceb3 0x2ecf7c27 0x2ecf7a0b 0x339f8283 0x3159b049 0xbe5e5 0x39b6dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
THX幫助!
實際上,這是我的JSON結構。我將解析器的「For循環」更改爲:for(NSString * place in [loadedContent allKeys]){ NSDictionary * info = [loadedContent objectForKey:place]; if([[info objectForKey:@「innov」] isEqualToString:nameInnov] == YES){ return info; } } Thx !! – testoverblaireau