簡體   English   中英

當NSMutableArray為plist時,App崩潰

[英]App Crashes when NSMutableArray to plist

我有一個名為*favoritesNSMutableArray 它是一個NSStrings數組,我想將它寫入.plist文件。

我打電話的時候:

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

它崩潰的異常類型為EXC_BAD_ACCESS (SIGBUS) ,異常代碼為KERN_PROTECTION_FAILURE at 0x00000006

我已經包含了crashlog,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];
}

您無法在iPhone上的應用程序包中編寫或修改文件。 如果要保存數據,請在應用程序的文檔目錄中創建文件。 您可以像這樣獲取文檔目錄:

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

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

查看Apple的低級文件管理編程主題,了解有關從應用程序保存數據的位置的更多信息。

這可能不是導致崩潰的原因,但將內容寫回捆綁包是非常糟糕的做法。 我甚至不確定是否有效。 如果應用程序包目錄是只讀的,那么您的應用程序可能會崩潰。

可以從應用程序包中讀取初始文件,但如果要更改並保留該數據,則必須將其寫入Documents目錄。 這在Stack Overflow上有很好的介紹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM