ios - Plist 从 URL 到 NSMutableArray

标签 ios objective-c plist nsdata nsurl

我有以下代码:

//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/

相关文章:

ios - 云端视频流不适用于 iOS 设备

objective-c - UILabel动画循环播放

python - 使用 python 子进程将 OSX plist 加载到 LaunchDaemons

SWIFT 属性列表 : From TableView to DetailView plist

iphone - FBFriendPickerViewController - 初始化之前选中的 friend

ios - iOS7 上的 UITextfield leftView/rightView 填充

ios - swift:聊天应用程序 - 单元格内的音频播放器

iphone - UIView 与核心图形

iphone - 在 Obj C 中使用 Brad Larson GPUImage 为照片创建素描效果

ios - Swift:阅读 plist - 如何按键过滤并仅读取和附加值