将 PList 信息传递给字符串,然后传递给可变数组 (iOS)

发布于 2024-12-18 08:00:50 字数 533 浏览 3 评论 0原文

我在将 plist 文件传输到可变数组中时遇到问题,因此我可以使用它来填充表视图。我创建了一个名为 MyDeals.plist 的 plist 。我将根设为一个数组,然后添加两个字典作为该数组的子集,然后用字符串填充这些字典。

代码如下:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MyDeals" ofType:@"plist"];
NSLog(@"%@", myfile);
NSMutableArray *myArray = [[NSMutableArray alloc] initWithContentsOfFile:myfile];
NSLog(@"%@", myArray);
self.dealsArray = myArray;
[myArray release];

第一个 NSLog 记录了 plist 文件的正确路径,因此它在此之前一直有效,但第二个日志返回“(null)”,因此 myArray 由于某种原因实际上并未从 myfile 中获取信息。有什么想法吗?谢谢!

I'm having trouble transferring a plist file into a mutable array so I can then use it to populate a table view. I created a plist called MyDeals.plist . I made the root an array and then added two dictionaries as subsets of that array and then populated those dictionaries with strings.

The code is as follows:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MyDeals" ofType:@"plist"];
NSLog(@"%@", myfile);
NSMutableArray *myArray = [[NSMutableArray alloc] initWithContentsOfFile:myfile];
NSLog(@"%@", myArray);
self.dealsArray = myArray;
[myArray release];

The first NSLog logs out the correct path for the plist file so it works up until then but then the second log returns '(null)' so myArray doesn't actually grab the info from myfile for some reason. Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-12-25 08:00:50

首先尝试将文件加载到 NSData 中,这样您就可以看到问题所在。

NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
if (!data) {
    NSLog(@"error loading plist from %@: %@", myfile, error);
    return;
}
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data
    options:NSPropertyListMutableContainers format:NULL error:&error];
if (!self.dealsArray) {
    NSLog(@"error parsing plist from %@: %@", myfile, error);
    return;
}

Try loading the file into an NSData first, so you can see what the problem is.

NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
if (!data) {
    NSLog(@"error loading plist from %@: %@", myfile, error);
    return;
}
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data
    options:NSPropertyListMutableContainers format:NULL error:&error];
if (!self.dealsArray) {
    NSLog(@"error parsing plist from %@: %@", myfile, error);
    return;
}
夢归不見 2024-12-25 08:00:50

我可能是错的,但我相信 plist 必须反序列化为字典而不是数组,即使根元素是数组。如果您想从文件中反串行化数组,它可能应该是从 NSArray writeToFileAtmoically 创建的文件。

I may be wrong but I believe a plist must be deserialized as a dictionary and not an array even if the root element is an array. If you want to desirialize an array from a file it probably should be a file that was created from NSArray writeToFileAtmoically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文