当 NSMutableArray 转为 plist 时应用程序崩溃

发布于 2024-10-03 19:58:56 字数 2960 浏览 2 评论 0原文

我有一个名为 *favoritesNSMutableArray。它是一个 NSStrings 数组,我想将其写入 .plist 文件。

当我调用时:

favPath = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"];
[favorites writeToFile:favPath automatically:YES];

它崩溃并出现异常类型 EXC_BAD_ACCESS (SIGBUS) 和异常代码 KERN_PROTECTION_FAILURE at 0x00000006

我已包含崩溃日志、Favorites.plist 和代码片段调用该方法的代码。非常感谢您的帮助!

崩溃日志:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000006
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x000034f8 objc_msgSend + 24
1   Foundation                      0x00011ae6 _NSWriteBytesToFileWithExtendedAttributes + 56
2   Foundation                      0x00011aa0 _NSWriteBytesToFile + 20
3   Foundation                      0x00011a60 -[NSData(NSData) writeToFile:atomically:] + 60
4   Foundation                      0x00049124 -[NSArray(NSArray) writeToFile:atomically:] + 148
5   CoderPlus                       0x00005a7a -[HTMLViewController tableView:didSelectRowAtIndexPath:] (HTMLViewController.m:277)
6   UIKit                           0x000c3f98 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 884
7   UIKit                           0x000d4084 -[UITableView _userSelectRowAtIndexPath:] + 196
8   Foundation                      0x00088334 __NSFireDelayedPerform + 360
9   CoreFoundation                  0x00074256 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 10
10  CoreFoundation                  0x00076966 __CFRunLoopDoTimer + 1038
11  CoreFoundation                  0x000773ea __CFRunLoopRun + 1178
12  CoreFoundation                  0x0001e0bc CFRunLoopRunSpecific + 220
13  CoreFoundation                  0x0001dfca CFRunLoopRunInMode + 54
14  GraphicsServices                0x00003f88 GSEventRunModal + 188
15  UIKit                           0x00007b40 -[UIApplication _run] + 564
16  UIKit                           0x00005fb8 UIApplicationMain + 964
17  CoderPlus                       0x0000837a main (main.m:14)
18  CoderPlus                       0x00002c38 start + 32

Favorites.plist

<?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">
<array>

</array>
</plist>

导致崩溃的方法:cellTitle 是一个 NSString 对象)

if (favButtonActive == [NSNumber numberWithInt: 1]) {
    // Write selection to Favorites.plist
    if ([favorites containsObject:cellTitle]) {
        [favorites removeObject:cellTitle];
    } else {
        [favorites addObject:cellTitle];
    }
    [favorites writeToFile:favPath atomically:YES];
    [table reloadData];
}

I have an NSMutableArray called *favorites. It is an array of NSStrings and I want to write it to a .plist file.

When I call:

favPath = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"];
[favorites writeToFile:favPath automatically:YES];

it crashes with the exception type EXC_BAD_ACCESS (SIGBUS) and with the exception code KERN_PROTECTION_FAILURE at 0x00000006

I have included the crashlog, the Favorites.plist, and a snippet of code where the method is being called. Help would be highly appreciated!

Crashlog:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000006
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x000034f8 objc_msgSend + 24
1   Foundation                      0x00011ae6 _NSWriteBytesToFileWithExtendedAttributes + 56
2   Foundation                      0x00011aa0 _NSWriteBytesToFile + 20
3   Foundation                      0x00011a60 -[NSData(NSData) writeToFile:atomically:] + 60
4   Foundation                      0x00049124 -[NSArray(NSArray) writeToFile:atomically:] + 148
5   CoderPlus                       0x00005a7a -[HTMLViewController tableView:didSelectRowAtIndexPath:] (HTMLViewController.m:277)
6   UIKit                           0x000c3f98 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 884
7   UIKit                           0x000d4084 -[UITableView _userSelectRowAtIndexPath:] + 196
8   Foundation                      0x00088334 __NSFireDelayedPerform + 360
9   CoreFoundation                  0x00074256 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 10
10  CoreFoundation                  0x00076966 __CFRunLoopDoTimer + 1038
11  CoreFoundation                  0x000773ea __CFRunLoopRun + 1178
12  CoreFoundation                  0x0001e0bc CFRunLoopRunSpecific + 220
13  CoreFoundation                  0x0001dfca CFRunLoopRunInMode + 54
14  GraphicsServices                0x00003f88 GSEventRunModal + 188
15  UIKit                           0x00007b40 -[UIApplication _run] + 564
16  UIKit                           0x00005fb8 UIApplicationMain + 964
17  CoderPlus                       0x0000837a main (main.m:14)
18  CoderPlus                       0x00002c38 start + 32

Favorites.plist

<?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">
<array>

</array>
</plist>

Method that's causing the crash: (cellTitle is an NSString object)

if (favButtonActive == [NSNumber numberWithInt: 1]) {
    // Write selection to Favorites.plist
    if ([favorites containsObject:cellTitle]) {
        [favorites removeObject:cellTitle];
    } else {
        [favorites addObject:cellTitle];
    }
    [favorites writeToFile:favPath atomically:YES];
    [table reloadData];
}

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

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

发布评论

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

评论(2

淡紫姑娘! 2024-10-10 19:58:57

您无法在 iPhone 上写入或修改应用程序包中的文件。如果要保存数据,请在应用程序的文档目录中创建文件。您可以像这样获取文档目录:

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

然后,只需使用 NSStringstringByAppendingPathComponent: 方法附加您的文件名。

查看 Apple 的低级文件管理编程主题,了解有关应用程序数据保存位置的更多信息。

You cannot write or modify a file in the application bundle on the iPhone. If you want to save data, create your file in the documents directory for your application. You can get the documents directory like this:

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

Then, just append your filename using NSString's stringByAppendingPathComponent: method.

Check out Apple's Low-Level File Management Programming Topics for more information on where to save data from your application.

小姐丶请自重 2024-10-10 19:58:57

这可能不是导致崩溃的原因,但将内容写回到包中是非常糟糕的做法。我什至不确定这是否有效。如果应用程序包目录是只读的,那么您的应用程序可能会因上述内容而崩溃。

可以从应用程序包中读取初始文件,但如果您想更改并保留该数据,则必须将其写入 Documents 目录。 Stack Overflow 上对此进行了详细介绍。

This is probably not the cause of your crash, but it is very bad practice to write stuff back to the bundle. I'm not even sure that works. If the app bundle directory is read-only then it is possible that your app crashes with the above.

It is fine to read an initial file from your app bundle, but if you want to change and persist that data then you must write it to the Documents directory. This is well covered here on Stack Overflow.

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