2010-11-21 6 views
3

NSMutableArray*favorites입니다. NSStrings의 배열이며 .plist 파일에 쓰고 싶습니다.App NSMutableArray가 plist 일 때 충돌이 발생합니다

내가 전화 할 때 :

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

는 예외 유형 EXC_BAD_ACCESS (SIGBUS)와 나는 crashlog의 Favorites.plist을 포함 한 예외 코드 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> 

방법 :

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]; 
} 
( cellTitleNSString 객체이다)

답변

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에서 잘 다루어집니다.

관련 문제