2010-11-21 15 views
3

NSMutableArrayという*favoritesがあります。それはNSStringsの配列であり、.plistファイルに書きたいと思います。アプリケーションがNSMutableArrayをplistにしたときにクラッシュする

私が呼ぶ:

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

それは私がcrashlog、Favorites.plist、どこでコードのスニペットが含まれている例外タイプEXC_BAD_ACCESS (SIGBUS)とし、例外コードKERN_PROTECTION_FAILURE at 0x00000006

でクラッシュメソッドが呼び出されています。ヘルプは高く評価されるだろう!

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> 

方法:cellTitleNSStringオブジェクトである)

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]; 
} 

答えて

2

iPhone上のアプリケーションバンドルにファイルを書き込んだり修正したりすることはできません。データを保存する場合は、アプリケーションのドキュメントディレクトリにファイルを作成します。あなたはこのようなドキュメントディレクトリを取得することができます。

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

をその後、ちょうどNSStringstringByAppendingPathComponent:方法を使用してファイル名を追加します。

アプリケーションのデータを保存する場所の詳細については、Appleの低レベルファイル管理プログラミングトピックを参照してください。

+0

私はこれを行い、まだ動作していません:NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory、NSUserDomainMask、YES); \t NSString * documentsDirectoryPath = [パスobjectAtIndex:0]; \t favPath = [documentsDirectoryPath stringByAppendingPathComponent:@ "Favorites.plist"]; \tお気に入り= [[NSMutableArray alloc] initWithContentsOfFile:favPath]; – Jakir00

+0

いくつかの質問:ファイルは存在しますか? 'NSArray'の' writeToFile:atomically: 'メソッドを使って書かれましたか?ファイルがplistの場合は、 'NSMutableArray'ではなく' NSMutableDictionary'に読み込む必要があります。 –

+0

アプリケーションの設定を保存する場合は、代わりに 'NSUserDefaults'を使用することを検討してください。 –

1

これはおそらくクラッシュの原因ではありませんが、バンドルに書き戻すことは非常に悪い習慣です。私はそれがうまくいくかどうかはわかりません。アプリバンドルディレクトリが読み取り専用の場合は、上記のようにアプリがクラッシュする可能性があります。

アプリのバンドルから最初のファイルを読み込んでも問題ありませんが、そのデータを変更して保存したい場合はディレクトリに書き込む必要があります。Documentsディレクトリに書き込んでください。これは、ここではStack Overflowで十分にカバーされています。

関連する問題