访问 NSMutableArray 时发生奇怪的崩溃

发布于 2024-11-10 00:51:07 字数 857 浏览 3 评论 0原文

我将对象添加到 NSMutableArray 并打印出其内容。

添加第一个对象时,它可以工作,并且数组显示计数为 1。

当我添加第二个对象时,显示数组的计数为 2,但是当之后直接访问该数组时,应用程序崩溃了。

[sharedsArray addObject:noteToAdd];

NSLog(@"The count of the array is %d", [sharedArray count]);

// Write the array to file
NSLog(@"Filepath is %@", filePath);
NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

应用程序在这两个语句中崩溃

 NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

由于访问sharedArray, 。我不明白为什么它在检查其计数时不会崩溃,但在检查其内容时会崩溃。

内容是 NSMutableDictionaries。

无法发布图像。

0 objc_msgSend
1<????>
2 _CFAppendXML0
3 _CFAppendXML0
4 _CFPropertyListCreateXMLData
5 CFPropertyListCreateXMLData
6 -[NSArray(NSArray)writeToFile:Atomically:]

I add objects to an NSMutableArray and print out its content.

When adding the first object it works and the array says count is 1.

When I add the second object, is shows the array has a count of 2, but when accessing the array directly after that the app crashes.

[sharedsArray addObject:noteToAdd];

NSLog(@"The count of the array is %d", [sharedArray count]);

// Write the array to file
NSLog(@"Filepath is %@", filePath);
NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

The app crashes on either of these 2 statements

 NSLog(@"shared array is %@", sharedArray);

[sharedArray writeToFile:filePath atomically:YES];

because of accessing the sharedArray. I dont see why it doesnt crash when checking its count, but it crashes when checking its contents.

The contents is NSMutableDictionaries.

Cant post images.

0 objc_msgSend
1<????>
2 _CFAppendXML0
3 _CFAppendXML0
4 _CFPropertyListCreateXMLData
5 CFPropertyListCreateXMLData
6 -[NSArray(NSArray)writeToFile:Atomically:]

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

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

发布评论

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

评论(1

離殇 2024-11-17 00:51:07

你有一个僵尸。您的sharedsArray需要保留。请参阅 这篇文章

您可以对它调用 count,因为 Objective-C 只是无操作地将消息发送到nil 对象,但直接引用该对象会导致崩溃。

You have a zombie. Your sharedsArray needs to be retained. See this post

You can call count on it because Objective-C just no-ops sending a message to a nil object, but a direct reference to the object causes a crash.

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