我正在尝试从ios中的plist中获取字典。问题是代码无效。它在不同的函数中工作,但不是这个。
NSDictionary* pdata=[NSDictionary dictionaryWithContentsOfFile:datapath];
该文件存在于路径中但由于某种原因它无法正常工作。当我尝试打印字典时,我得到null。
编辑:这是我的路径/私人/ var / mobile / Applications / F4D5EFE8-58FC-4179-9492-4C4F9FD01024 / Library / Preferences / abc.def.ghi.plist。这是一个jailbroken应用程序,我能够在一个单独的位置成功完成另一个pplist。
答案 0 :(得分:1)
这应该工作正常。
NSString *plistFileUrl=[[NSBundle mainBundle] pathForResource:@"someplistFile" ofType:@"plist"];
NSURL *url=[NSURL fileURLWithPath:plistFileUrl];
NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:url];
答案 1 :(得分:0)
这一定对你有用。
NSString *aStr=[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
NSURL *aUrl=[NSURL fileURLWithPath:aStr];
NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:aUrl];
答案 2 :(得分:0)
试试这个。
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"example" ofType:@"plist"]isDirectory:NO]];
NSLog(@"data is %@",dict);
答案 3 :(得分:0)
试试这个
//Local mainbundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [path stringByAppendingPathComponent:@"Contents.plist"];
NSMutableDictionary *dictContent = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@",dictContent);
///DocumentsDirectory
NSMutableDictionary *dictItem=[[NSMutableDictionary alloc]init];
[dictItem setObject:@"ravi" forKey:@"name"];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *docFilePath = [documentsDirectoryPath
stringByAppendingPathComponent:@"data.plist"];
[dictItem writeToFile:docFilePath atomically: YES];
NSMutableDictionary *dictContent1 = [NSMutableDictionary dictionaryWithContentsOfFile:docFilePath];
NSLog(@"%@",dictContent1);