Ios Plist从URL到NSMutableArray
我的代码如下:Ios Plist从URL到NSMutableArray,ios,objective-c,plist,nsdata,nsurl,Ios,Objective C,Plist,Nsdata,Nsurl,我的代码如下: //Find device directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //Define file name
//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”with string:@']
返回一个新的NSString
。它不会改变noSpaces
更改这两行:
[noSpaces stringByReplacingOccurrencesOfString:@"plist" withString:@""];
NSURL *url = [[NSBundle mainBundle] URLForResource:noSpaces withExtension:@"plist"];
致:
更新:为什么不使用filePath将内容读取到NSMutableArray
NSURL *url = [NSURL URLWithString:filePath];
注:
1) [[NSBundle mainBundle]URLForResource:withExtension:]
用于查找内置到应用程序中的资源。
2) 如果文件的内容无法解析为数组,则仍然会得到nil
这假定文件位置正确。如果您要做的是加载远程URL并将内容保存到plist文件中,然后读取它,则应确保1)内容成功保存到文件中,2)加载路径正确,3)可以将文件内容解析为数组。mapDetails仍然返回“nil”-很抱歉。您应该在
mapDetails=…
行设置断点,检查url值,并确保它对于文件位置是正确的。请在主问题帖子中更新。如何“使用filePath将内容读取到NSMutableArray”??抱歉,我有点新手:)我已经检查了iOS目录@JamesChan,Plist在那里。如何从目录中加载Plist并将其内容放入数组??感谢您的帮助(顺便说一句:)使用[NSURL URLWithString:filePath]
作为文件URL。当代码无法按预期工作时,最好将长行拆分为多行。每行进行一次方法调用,并将结果分配给变量。它使代码更易于阅读和调试。将包含5个方法调用的行拆分为5行单独的代码。
NSURL *url = [NSURL URLWithString:filePath];