2013-10-18 2 views
0

많은 질문/자습서를 읽으려고했지만 대답을 찾을 수 없습니다!.plist (특성 목록) 파일에 NSString 추가

배열이

//Save Name to plist 
     if([name.text length] > 0){ 
      NSString *string = name.text; 
      [array addObject:string]; 
      //plist path 
      NSString *path = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"]; 
      //save object 
      [array writeToFile: path atomically:YES]; 

     } 

입니다 : 나는 텍스트 필드로부터 문자열을 얻고 (기본 키 루트에서) 내가 만든 datas.plist 파일에 저장하기 위해 노력하고있어 나는이 코드를 시도했습니다 내가 WR 코드를 생각

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'

:

@interface DetailViewController(){ 
    NSMutableArray* array; 
} 

는 그러나 응용 프로그램 충돌이 오류를 제공 (내가 버튼에 연결하기 전에 쓴 작업)을 저장하려고하면 내가 키를 입력하지 않기 때문에 ites가 올바르지 않습니다 (예 : @ "Root") 또는 이와 비슷한. 사전에 감사

편집 :

나는이 코드를 시도했지만 아무것도 기록하지 않습니다

- (void)addToMyPlist { 
    NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    destPath = [destPath stringByAppendingPathComponent:@"dati"]; 

    // If the file doesn't exist in the Documents Folder, copy it. 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:destPath]) { 
     NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"]; 
     [fileManager copyItemAtPath:sourcePath toPath:destPath error:nil]; 
    } 

    // Load the Property List. 
    array = [[NSMutableArray alloc] initWithContentsOfFile:destPath]; 

     //NSString *path = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"]; 
     NSString *string = self.name.text; 

     NSArray *values = [[NSArray alloc] initWithObjects:string, nil]; 
     NSArray *keys = [[NSArray alloc] initWithObjects:@"Root", nil]; 
     NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 
     [array addObject:dict]; 
     [array writeToFile:destPath atomically:YES]; 
} 

잘못 무엇을?

+3

내가 원만이이 스택 오버플로 ... –

+3

귀하의 PLIST 문제와 예외 (코어 데이터)에 요청 때마다 가지고있는 경우 것 같다 관련이없는 것 ... – Wain

+0

@Bigood 그래, 내가 감사 했어 – r4id4

답변

1

번들의 모든 파일은 읽기 전용입니다.

쓰기 권한이 있으려면 앱의 비공개 문서 (또는 다른 3 개가 있음) 폴더에서 pList를 이동해야합니다.

예를 들어

이 문서 디렉토리에 번들 파일을 복사 :

if([name.text length] > 0){ 
     NSString *string = name.text; 
     [array addObject:string]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     //Path to document directory 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 

     //Path to your datas.plist inside documents directory 
     NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"datas.plist"]; 

     //If the file doesn't exists yet 
     if ([fileManager fileExistsAtPath:documentsPath] == NO) { 
      //Let's copy it in your Documents folder 
      NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"]; 

      [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]; 
     } 

     [array writeToFile: documentsPath atomically:YES]; 

    } 
+0

나는 Xcode newFile>에서 plist 파일을 만들었다. .. 그리고 그것은 내 응용 프로그램의 폴더에 기본적으로 작성되었다고 생각한다. – r4id4

+0

나는 당신의 코드를 바꾸려고했지만 항상이 오류를 준다. : *** 캐치되지 않은 예외 'NSInvalidArgumentException'으로 인해 앱 종료 중, 이유 : 'NSPersistentStoreCoord를 만들 수 없음 iner with nil model ' – r4id4

+1

@ObiWanKeNerd이 오류는 CoreData에 연결됩니다. 그것을 사용하지 않거나 AppDelegate에서 Coredata init 함수를 제거하려고 시도하십시오. – Bigood

관련 문제