访问 NSMutableArray 时发生奇怪的崩溃
我将对象添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有一个僵尸。您的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.