Iphone NSMutableDictionary initWithContentsOfFile-null

Iphone NSMutableDictionary initWithContentsOfFile-null,iphone,ios,Iphone,Ios,我正在尝试创建一个包含.plist内容的NSMutableDictionary。我有以下代码: NSString *myFile = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"]; NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile]; NSObject *randomWord = [myD

我正在尝试创建一个包含.plist内容的NSMutableDictionary。我有以下代码:

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"];
NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile];
NSObject *randomWord = [myDict objectForKey:@"Item 6"];
NSLog(@"%@", randomWord);
myFile和myDict是填充的(不是null),但是当想要打印randomWord(或者myDict)时,它会打印“(null)”

我到处都找过了。我做错了什么

编辑:

以及NSLog:

2012-05-10 12:26:22.302 project2[681:f803] (null)
看来myFile和myDict都被填满了,但randomWord没有

编辑#2:

NSLog(@“%@”,myDict)打印:


你说得对,myDict是空的。

删除第6项中的空格

[myDict objectForKey:@"Item6"];
如果这个不行,试试这个

NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"];
NSData* data = [NSData dataWithContentsOfFile:path];
NSMutableArray* array = [NSPropertyListSerialization propertyListFromData:data
                                                         mutabilityOption:NSPropertyListImmutable
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:NULL];
if (array) {
  NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
  for (NSDictionary* dict in array) {
    State* state = [[State alloc] initWithDictionary:dict];
    [myDict setObject:state forKey:state.name;
    [state release];
  }
  NSLog(@"The count: %i", [myDic count]);
} else {
  NSLog(@"Plist does not exist");
}

当项目应该是单词时,为什么要将其强制转换为NSObject而不是NSString?你能发布myDict的日志吗?我读到你的问题是,
myDict
既不是空的,也不是空的。应该是哪一个?Johannes,日志发布。菲利普,你说得对,对不起。myDict不是空的,但randomWord是空的。请参阅编辑。如果这确实是Johannes要求的日志,那么与您所说的相反,
myDict
为空。在这种情况下,我更不明白……您是指“状态”还是“NSState”?当我使用“State”时,Xcode错误表明“State”不是他能识别的东西。哦,就这点而言,“NSState”也不存在。哦,这些项目被称为“Item x”,包括空格Item6'不起作用。state是用于组合值及其键的任何对象jst,不是预定义的,state是用于存储值和键的对象jst,不是预定义的
[myDict objectForKey:@"Item6"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"];
NSData* data = [NSData dataWithContentsOfFile:path];
NSMutableArray* array = [NSPropertyListSerialization propertyListFromData:data
                                                         mutabilityOption:NSPropertyListImmutable
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:NULL];
if (array) {
  NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
  for (NSDictionary* dict in array) {
    State* state = [[State alloc] initWithDictionary:dict];
    [myDict setObject:state forKey:state.name;
    [state release];
  }
  NSLog(@"The count: %i", [myDic count]);
} else {
  NSLog(@"Plist does not exist");
}