iPhone Plist 转 NSDictionary 显示黑屏

发布于 2024-11-05 21:44:35 字数 1569 浏览 2 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(1

你列表最软的妹 2024-11-12 21:44:35

像这样更改代码:

NSPropertyListFormat format;

NSMutableDictionary *plist = [NSPropertyListSerialization propertyListFromData:data 
                                                              mutabilityOption:0 
                                                                        format:&format
                                                              errorDescription:&error];

即删除第一个 format 前面的 * 并将 & 添加到第二个

NSPropertyListFormat 不是一个对象,它是一个枚举,并且 propertyListFromData: mutabilityOption:format:errorDescription: 想要一个指向 NSPropertyListFormat 实例的指针。

您传递了一个未初始化的指针,并且该方法尝试写入这个“随机”地址。这导致了崩溃。

Change your code like this:

NSPropertyListFormat format;

NSMutableDictionary *plist = [NSPropertyListSerialization propertyListFromData:data 
                                                              mutabilityOption:0 
                                                                        format:&format
                                                              errorDescription:&error];

I.e. remove the * in front of the first format and add a & to the second one

NSPropertyListFormat is not an object, it is an enum, and propertyListFromData: mutabilityOption:format:errorDescription: wants a pointer to an instance of NSPropertyListFormat.

You were passing a not initialized pointer, and the method tried to write to this "random" address. Which lead to a crash.

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