iPhone Plist 转 NSDictionary 显示黑屏
我正在开发一个 iPhone 应用程序,我的目的是从 Web 服务器检索 plist 并在表格视图中显示结果。
我在尝试反序列化 plist 时遇到问题。下面的代码可以编译,但在运行时 iPhone 模拟器屏幕保持黑色,在我看来,视图永远不会被加载,并且 NSLog 消息不会显示在控制台中。另一方面,如果我调试消息确实会打印在控制台中,但视图不会出现,屏幕保持黑色:
- (void)viewDidLoad {
NSData *data = [@"<?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\"><dict><key>count</key><array><integer>5</integer></array><key>username</key><array><string>johnsmith</string></array></dict></plist>" dataUsingEncoding:NSUTF8StringEncoding];
NSString *error = nil;
NSPropertyListFormat *format;
NSMutableDictionary *plist = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:0
format: format
errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file, error = '%s'", [error UTF8String]);
[error release];
} else {
NSLog(@"Groovy");
NSLog(@"Count: %d",[plist count]);
NSLog(@"Username %@", [plist objectForKey:@"username"]);
NSLog(@"Count %@", [plist objectForKey:@"count"]);
//[plist retain];
}
//[plist dealloc];
[super viewDidLoad];
}
我一直在研究,他们说 NSPropertyListSerialization 泄漏内存,我不确定这是否是这样我的情况。如果您想尝试此代码,我刚刚创建了一个基于 iphone 视图的应用程序并将代码粘贴到 (void)viewDidLoad 方法中。
任何帮助将不胜感激。
I am developing an iphone application, my intent is to retrieve a plist from a web server and show the results in an tableview.
I am having an issue when trying to deserialize the plist. The following code compiles but at run time the iphone simulator screen stays black, it seems to me that the view never gets loaded and the NSLog messages don´t show up in the console. On the other hand if I debug the messages do get printed in the console but the view doesn´t come up the screen stays black:
- (void)viewDidLoad {
NSData *data = [@"<?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\"><dict><key>count</key><array><integer>5</integer></array><key>username</key><array><string>johnsmith</string></array></dict></plist>" dataUsingEncoding:NSUTF8StringEncoding];
NSString *error = nil;
NSPropertyListFormat *format;
NSMutableDictionary *plist = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:0
format: format
errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file, error = '%s'", [error UTF8String]);
[error release];
} else {
NSLog(@"Groovy");
NSLog(@"Count: %d",[plist count]);
NSLog(@"Username %@", [plist objectForKey:@"username"]);
NSLog(@"Count %@", [plist objectForKey:@"count"]);
//[plist retain];
}
//[plist dealloc];
[super viewDidLoad];
}
I´ve been researching and they say that the NSPropertyListSerialization leaks memory, I am not sure if that´s my case. In case you want to try out this code I just created an iphone view based application and pasted the code into the (void)viewDidLoad method.
Any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样更改代码:
即删除第一个
format
前面的*
并将&
添加到第二个NSPropertyListFormat
不是一个对象,它是一个枚举,并且propertyListFromData: mutabilityOption:format:errorDescription:
想要一个指向NSPropertyListFormat
实例的指针。您传递了一个未初始化的指针,并且该方法尝试写入这个“随机”地址。这导致了崩溃。
Change your code like this:
I.e. remove the
*
in front of the firstformat
and add a&
to the second oneNSPropertyListFormat
is not an object, it is an enum, andpropertyListFromData: mutabilityOption:format:errorDescription:
wants a pointer to an instance ofNSPropertyListFormat
.You were passing a not initialized pointer, and the method tried to write to this "random" address. Which lead to a crash.