我有以下代码:
//Find device directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Define file name
NSString * fileName = [file objectForKey: (id) kCFFTPResourceName];
//File path to file
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,fileName];
//Download Plist from server and write to 'filePath'
[[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://url.net/directory/%@",[file objectForKey: (id) kCFFTPResourceName]]]] writeToFile:filePath atomically:YES];
//Load Plist from directory
NSURL *url = [NSURL URLWithString:filePath];
//Load Data into Array
mapDetails = [[NSMutableArray alloc ] initWithContentsOfURL:url];
NSLog(@"Here is the Dict %@",mapDetails);
我的问题是,array
返回为“nil”,我的问题是什么??
我尝试了各种组合,但仍然无法将 PLIST
中的字符串放入 NSMutableArray
中。
最佳答案
[noSpaces stringByReplacingOccurrencesOfString:@"plist"withString:@""];
返回一个新的 NSString
。它不会更改 noSpaces
。
改变这两行:
[noSpaces stringByReplacingOccurrencesOfString:@"plist" withString:@""];
NSURL *url = [[NSBundle mainBundle] URLForResource:noSpaces withExtension:@"plist"];
到:
NSURL *url = [[NSBundle mainBundle] URLForResource:[noSpaces stringByReplacingOccurrencesOfString:@".plist" withString:@""] withExtension:@"plist"];
更新:为什么不用filePath来读取内容到NSMutableArray
?
NSURL *url = [NSURL URLWithString:filePath];
注意:
1) [[NSBundle mainBundle] URLForResource: withExtension:]
用于查找应用程序内置的资源。
2) 如果文件的内容无法解析为数组,您仍然会得到nil
。
这假设文件位置是正确的。如果你做的是加载远程 URL 并将内容保存到 plist 文件,然后读取它,你应该确保 1) 内容成功保存到文件,2) 加载它的路径是正确的,3) 文件内容可以解析为数组。
关于ios - Plist 从 URL 到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558682/