使用 XML plist 时 UITableView 在滚动时崩溃

发布于 2024-10-29 08:31:50 字数 1298 浏览 4 评论 0原文

我已经阅读了几个小时,搜索了苹果的文档,stackoverflow,无法理解我做错了什么......

当我在 UITableViewController 上使用来自 XML plist 的数据时:

- (void)viewDidLoad
    {
        [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"arrayofstrings"
                                                             ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        NSString *error;
        NSPropertyListFormat format;

        myArray = [NSPropertyListSerialization propertyListFromData:myData
                                                       mutabilityOption:NSPropertyListImmutable
                                                                 format:&format
                                                       errorDescription:&error];
    }

我的 tableview 显示了第一个可见行就好了但在尝试滚动时崩溃。

当我使用类似这样的东西而不是 XML 数据时,就不会发生这种情况:

- (void)viewDidLoad
        {
            [super viewDidLoad];

        myArray = [[NSArray alloc] initWithObjects:@"thing1", @"thing2", @"thing3", @"thing4", @"thing5",@"thing6", @"thing7", @"thing8", @"thing9", @"thing10", 
            @"thing11",@"thing12", @"thing13", @"thing14", nil];

        }

这样表视图就可以很好地滚动。我有什么问题吗? plist 到数组的转换是否应该以其他方式进行?

i've been reading for hours, searched apple's doc, stackoverflow, can't understand what i'm doing wrong....

when i use this data from a XML plist on my UITableViewController:

- (void)viewDidLoad
    {
        [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"arrayofstrings"
                                                             ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        NSString *error;
        NSPropertyListFormat format;

        myArray = [NSPropertyListSerialization propertyListFromData:myData
                                                       mutabilityOption:NSPropertyListImmutable
                                                                 format:&format
                                                       errorDescription:&error];
    }

my tableview shows the first visible rows just fine but crashes when trying to scroll.

it doesn't happen when instead of the XML data i use something like this:

- (void)viewDidLoad
        {
            [super viewDidLoad];

        myArray = [[NSArray alloc] initWithObjects:@"thing1", @"thing2", @"thing3", @"thing4", @"thing5",@"thing6", @"thing7", @"thing8", @"thing9", @"thing10", 
            @"thing11",@"thing12", @"thing13", @"thing14", nil];

        }

this way the tableview scrolls just fine. what's my problem?! Is the plist conversion to array supposed to be in any other way?

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

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

发布评论

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

评论(2

坐在坟头思考人生 2024-11-05 08:31:50
- (void)viewDidLoad
    {
        [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"arrayofstrings"
                                                             ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        NSString *error;
        NSPropertyListFormat format;

        myArray = [[NSPropertyListSerialization propertyListFromData:myData
                                                       mutabilityOption:NSPropertyListImmutable
                                                                 format:&format
                                                       errorDescription:&error] retain];
    }

propertyListFromData:mutabilityOption:format:errorDescription 的返回值为 autoreleased。确保你调用了retain,这样它就不会在当前运行循环结束时从你下面释放出来。

第二种方法有效,因为使用 alloc/init 创建 NSArray 会使数组的保留计数为 1。

- (void)viewDidLoad
    {
        [super viewDidLoad];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"arrayofstrings"
                                                             ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        NSString *error;
        NSPropertyListFormat format;

        myArray = [[NSPropertyListSerialization propertyListFromData:myData
                                                       mutabilityOption:NSPropertyListImmutable
                                                                 format:&format
                                                       errorDescription:&error] retain];
    }

The return value from propertyListFromData:mutabilityOption:format:errorDescription is autoreleased. Make sure you call retain so it doesn't get released out from under you at the end of the current run loop.

The second method works because creating the NSArray with alloc/init leaves the array with a retain count of 1.

甜`诱少女 2024-11-05 08:31:50

问题是,在第一种情况下,您对 [NSPropertyListSerialization propertyListFromData: 的调用返回一个没有保留计数的 NSArray (请注意,该方法的名称中没有 alloc、new 或 copy) -然后你就不再保留这个 NSArray 了。因此,数组很快就会被释放,并且您的代码在尝试访问垃圾内存时崩溃。

在第二种情况下,您正在使用 alloc 创建一个 NSArray - 这将返回一个保留计数为 1 的 NSArray,这意味着它不会被释放(直到在某个时刻调用释放)。

要解决此问题,在第一种情况下,您需要按如下方式分配数组:

self.myArray = ...

self. 是此处的关键部分(假设您已将 myArray 属性声明为 retain) 。

有大量关于内存的资源和博客文章管理。

The problem is that in the first case your call to [NSPropertyListSerialization propertyListFromData: returns an NSArray with no retain count on it (note the method doesn't have alloc, new, or copy in the name) - and then you don't retain this NSArray. Hence, the array is getting deallocated shortly after, and your code crashes trying to access garbage memory.

In the second case, you are making an NSArray using alloc - this returns an NSArray with a retain count of 1, which means it isn't deallocated (until a release gets called at some point).

To fix this, in your first case you want to assign the array as follows:

self.myArray = ...

The self. is the crucial part here (assuming you have declared the myArray property as retain).

There's plenty of resources and blog posts available concerning memory management.

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