NSPropertyListSerialization 无法为表视图中的详细信息视图设置图像字符串 (iOS)
当我浏览表视图时,我无法保留数组的内容。我的意思是,当我单击表视图中的一行时,我将尝试从 PList 中提取详细信息视图的数据。这是我在 viewDidLoad 中的内容:
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MillersDeals" ofType:@"plist"];
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:&error];
NSLog(@"%@", self.dealsArray);
现在我从未使用过 NSPropertyListSerialization ,直到早些时候我被建议将其作为问题的答案。我不确定它是否会产生一整套不同的问题,或者是否需要将其转换回字典或数组才能使用其中的信息。当我两次 NSLog 时它都会打印出我的 Plist 。在我写的 didSelectRowAtIndexPath 方法上(我尝试将 PList 中的“图像”传递到我在详细信息视图中设置的图像字符串中):
DealsDetailViewController *dealsDetail = [[DealsDetailViewController alloc]initWithNibName:@"DealsDetailViewController" bundle:nil];
NSLog(@"%@", dealsArray);
dealsDetail.petImageString = [[NSString alloc]initWithString:[[[dealsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Image"]];
错误出现在 DealsDetail.petImageString 声明上。我设置的 DealsArray 的数据类型是否错误?
Plist 示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Description</key>
<string>Here's the deal</string>
<key>Image</key>
<string>Frito-Lays-Jobs.png</string>
<key>Name</key>
<string>Frito-Lays</string>
<key>Thumbnail</key>
<string>dollars.png</string>
</dict>
<dict>
<key>Description</key>
<string>Here's the deal 2</string>
<key>Image</key>
<string>Frito-Lays-Jobs.png</string>
<key>Name</key>
<string>Frito-Lays 2</string>
<key>Thumbnail</key>
<string>dollars.png</string>
</dict>
</array>
这只是示例(这就是为什么某些元素是相同的)。
I'm having trouble holding on to the contents of an array as I go through a Table View. What I mean is that I have a PList that I'm trying to draw data from for a Detail View once I click on a row in a Table View. Here's what I have in viewDidLoad:
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"MillersDeals" ofType:@"plist"];
NSError *error;
NSData *data = [NSData dataWithContentsOfFile:myfile options:0 error:&error];
self.dealsArray = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainers format:NULL error:&error];
NSLog(@"%@", self.dealsArray);
Now I've never used NSPropertyListSerialization until earlier when I was suggested it as an answer to a question. I'm not sure if it creates a whole set of different issues or if it needs to be converted back to a Dictionary or Array before I can utilize the information within. It prints out my Plist ok when I NSLog it both times. on the didSelectRowAtIndexPath method I wrote this (I attempt to pass the 'image' from the PList into an Image String that I've set up in the Detail View):
DealsDetailViewController *dealsDetail = [[DealsDetailViewController alloc]initWithNibName:@"DealsDetailViewController" bundle:nil];
NSLog(@"%@", dealsArray);
dealsDetail.petImageString = [[NSString alloc]initWithString:[[[dealsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"Image"]];
The error comes on the dealsDetail.petImageString declaration. Is it the wrong data type that I've set dealsArray to?
Sample of Plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Description</key>
<string>Here's the deal</string>
<key>Image</key>
<string>Frito-Lays-Jobs.png</string>
<key>Name</key>
<string>Frito-Lays</string>
<key>Thumbnail</key>
<string>dollars.png</string>
</dict>
<dict>
<key>Description</key>
<string>Here's the deal 2</string>
<key>Image</key>
<string>Frito-Lays-Jobs.png</string>
<key>Name</key>
<string>Frito-Lays 2</string>
<key>Thumbnail</key>
<string>dollars.png</string>
</dict>
</array>
It's just sample stuff (that's why some of the elements are the same).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您显示的这个 plist 是一个字典数组,而不是字典数组的数组。所以像这样的东西应该在你的 didSelectRowAtIndexPath 中工作:
This plist you showed is an array of dictionaries, not an array of an array of dictionaries. So something like this should work in your didSelectRowAtIndexPath:
只是一个观察,但在第一个代码片段中您设置了“dealsArray”,而在第二个代码片段中,您尝试访问“dataArray”中的信息。 “dealsArray”的内容在什么时候被解析或传递到“dataArray”?您是否设置和/或访问了错误的数据结构?
Just an observation, but in the first code snippet you set a 'dealsArray' whereas in the second code snippet, you try to access the info in 'dataArray'. At what point does the contents of 'dealsArray' get parsed or passed onto 'dataArray'? Are you setting and/or accessing the wrong data structure(s) perhaps?