Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从plist创建nsarray时出错?_Iphone_Objective C_Cocoa Touch_Nsarray - Fatal编程技术网

Iphone 从plist创建nsarray时出错?

Iphone 从plist创建nsarray时出错?,iphone,objective-c,cocoa-touch,nsarray,Iphone,Objective C,Cocoa Touch,Nsarray,我正在尝试从.plist文件创建NSArray,但不断出现以下错误: “'NSUnknownKeyException',原因:”[valueForUndefinedKey:]:此类不符合键值1的键值编码要求。” 在.plist文件中,我有一个名为“Item 1”的键和一个名为“Value1”的字符串值 然后在代码中,我从该文件创建了一个NSArray: -(void)recordValues:(id)sender { // read "propertyList.plist" value

我正在尝试从.plist文件创建NSArray,但不断出现以下错误:

'NSUnknownKeyException',原因:”[valueForUndefinedKey:]:此类不符合键值1的键值编码要求。

在.plist文件中,我有一个名为“Item 1”的键和一个名为“Value1”的字符串值 然后在代码中,我从该文件创建了一个NSArray:

-(void)recordValues:(id)sender {

    // read "propertyList.plist" values
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"propertyList" ofType:@"plist"];
    originalArray = [[NSArray alloc] initWithContentsOfFile:plistPath];

   // dump the contents of the array to the log
    for (id key in originalArray) {
        NSLog(@"bundle: key=%@, value=%@", key, [originalArray valueForKey:key]);
    }

    //create an NSMutableArray containing the
    //user's score and the values from originalArray
    NSMutableArray *newArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:userScore], nil];
    [newArray addObjectsFromArray:array];

    //write xml representation of mutableArray back to propertyList
    [newArray writeToFile:plistPath atomically:NO];

}

    }

for
循环中,您像使用字典一样使用
originalArray
。如果plist的根是字典,则应该使用
NSDictionary
,而不是
NSArray
来读取它。

数组没有键(它们不需要任何键,因为它们的元素由索引寻址),只有字典由键值对组成。尝试:

for (id value in originalArray) {
            NSLog(@"bundle: value=%@", value);
    }

您可以立即使用NSMUTABLEARRY而不是NSArray,并将其转换为NSMUTABLEARRY,同时确保pList是数组类型。您的代码如下所示:

-(void)recordValues:(id)sender {
// read "propertyList.plist" values
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"propertyList" ofType:@"plist"];
//create an NSMutableArray containing the
//user's score and the values from originalArray
NSMutableArray *array = [NSArray arrayWithContentsOfFile: path];
[newArray addObjectsFromArray:array];
//check-out the content
NSLog(@"The content of the array: %@", array);
//write xml representation of mutableArray back to propertyList
[array writeToFile:plistPath atomically:NO];
}
}

根是一个不安全的地方。那我怎么把它理解为一种诅咒呢?我需要添加值,但我不能使用NSDictionary添加值。确切地说,如果根是“Array”,那么您可以使用NSArray或NSMutableArray。说实话,我就是说不出您想做什么。请用更精确一点的语言,好吗?当然!我有一个属性列表,它是一个数组。这些基本上就像游戏中的高分。当有人玩游戏时,我会将他们的分数添加到一个全新的NSMutableArray中。然后,我将这个新的可变数组与属性列表中的高分结合起来,并将整个集合写回原始plist文件。但我有一个错误,我不知道为什么。这有意义吗?我忘了我在那里留下了一个旧变量,如果你不知道应用程序的功能,这就没有意义了。:)很抱歉修复了它。仅供参考,如果这样做,您可以简化代码并使用单个变量:
NSMutableArray*array=[NSMutableArray arraywhithcontentsoffile:plistPath]现在,您正在泄漏原始阵列。