数组无法从plist中读取数据

时间:2012-05-19 19:19:02

标签: iphone objective-c xcode plist

我发现了几个类似的话题,但没有一个能解决我的问题。 基本上我想在plist中从我的数组中读取nsstring,然后我想覆盖nsstring值。

NSArray *directoriespaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDir = [directoriespaths objectAtIndex:0];
NSString *fullnamefordir = [documentsDir stringByAppendingPathComponent:@"data.plist"];

        NSArray *turns = [NSArray arrayWithContentsOfFile:fullnamefordir];
        NSString *resultofarray = [turns objectAtIndex:0];
        NSLog(@"%@",turns); 
        NSLog(@"%@",resultofarray); 

nslog总是显示为null,我到目前为止并没有尝试保存任何内容,因为我甚至无法读取该值:/

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>8</string>
</array>
</plist>

我也尝试过使用nsdictionary,但它也不起作用 提前谢谢:)

1 个答案:

答案 0 :(得分:3)

首先检查您的plist文件是否存在于文档目录中。

NSFileMangaer *objFileManager=[[NSFileManager alloc]init];
BOOL isfileExist=[objFileManager fileExistsAtPath:yourDocumentDirectoryPath];

如果Bool值为true,则检查plist的内容。 可能是文件目录中不存在的文件,它在资源中。 如果您没有将plist从资源文件夹复制到文档目录,那么首先将其复制到那里,然后尝试访问它。

相关问题