简体   繁体   English

数组无法从plist读取数据

[英]array can't read data from plist

I have found couple of similar topic about this but none of them solve my problem. 我已经找到了两个类似的话题,但是没有一个能解决我的问题。 basically I want to read nsstring from my array in plist and then I want to overwrite the nsstring value. 基本上,我想从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 always shows null and i'm not trying to save anything so far because i can't even read the value :/ 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>

i also tried with nsdictionary but it also doesn't work thanks in advance :) 我也尝试了nsdictionary,但也不能正常工作,谢谢:)

First Check whether your plist file exist in Document Directory or not. 首先检查您的plist文件是否存在于“文档目录”中。

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

IF Bool Value is true then check you content of plist. 如果布尔值是true,则检查plist的内容。 May be the file doesnt exist in document directory ,it is in resource. 可能是文件不在文档目录中,而是在资源中。 If you didnot copied you plist from resource folder to document directory then first copy it there and then try to access it. 如果您没有将plist从资源文件夹复制到文档目录,则首先将其复制到那里,然后尝试访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM