2012-03-12 3 views
0

필자의 데이터를 plist에 복사하려고했지만이 코드를 추가하는 경우 작동하지 않습니다. 어느 것이 든 내가 데이터를 추가하는 좋은 방법을 제안합니까?
NSMutableDictionary * nameDictionary = [NSMutableDictionary 사전];아무도 나를 plist 파일에 데이터를 추가하는 데 도움이 될 수 있습니까?

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
[nameDictionary setValue:nameEntered.text forKey:@"name"]; 
[nameDictionary setValue:company.text forKey:@"company"]; 
[nameDictionary setValue:designation.text forKey:@"designation"]; 

// create dictionary with values in UITextFields 
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [NSMutableArray array]; 
[plist addObject:nameDictionary]; 
[plist writeToFile:plistPath atomically:YES]; 
UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"succsefully created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[tellErr show]; 
[email protected]""; 
[email protected]""; 
[email protected]""; 
+0

작동하지 않는다고 말하면 충돌이 발생 했습니까? 아니면 배열 끝에 단순히 추가 할 것으로 예상되는 사전이 없습니까? 위에서 보았 듯이 위의 코드는 괜찮아 보입니다. – Sam

+0

plist에서 전체 데이터를 처음으로 삽입하려고 할 때 또는 목록을보고 나서 삽입하려고 할 때 – ashwin19

+0

질문에 답변하는 경우 녹색으로 확인하십시오. –

답변

1

+[NSKeyedArchiver archiveRootObject:toFile:]을 사용하여 Array 개체를 파일로 보관해야합니다. 그리고 보관하려면 +[NSKeyedUnarchiver unarchiveObjectWithFile:]을 사용하십시오.

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"]; 
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
if (plist == nil) plist = [[NSMutableDictionary alloc] init]; 
[plist setObject:nameEntered.text forKey:@"name"]; 
[plist setObject:company.text forKey:@"company"]; 
[plist setObject:designation.text forKey:@"designation"]; 
[plist writeToFile:plistPath atomically:YES]; 

... 등이 ... 물론

, 이것은 정확히 당신이 달성하려는 작업에 따라 달라집니다

+0

이미 [NSKeyedArchiver archiveRootObject : toFile :]'오퍼를 사용하면 어떤 이점이 있습니까? – Sam

0

나는 갈 것이다.

+0

하지만 이전 데이터를 덮어 씁니다. 새로 입력 한 데이터를 이전 데이터에 추가하려고합니다. – ashwin19

+0

이전 데이터를 덮어 쓰지 않습니다. 그러나 1 분 정도 후에 해결할 오류가 있습니다. –

+0

좋아요, 오류가 수정되었습니다. dictionayWithContentsOf 파일을 저장하고 사전을 초기화하고 파일의 내용을 사전에 읽습니다. 다음 plist 값은 사전에 있습니다. 그런 다음 3 개의 새 값이 추가 된 후 전체 사전이 동일한 파일 이름에 기록됩니다. 트릭을해야 할 것입니다. –

0

iOS의 응용 프로그램 기본 번들에 쓸 수 없습니다. 쓰기 가능 디렉토리에 특성 목록 파일을 복사해야합니다. 그런 다음 그것을 읽고 수정하고 다시 쓸 수 있습니다.

+0

당신은 내가 그 문서 디렉토리 또는 그 폴더와 같은 폴더에서 plist 파일을 가져 오라고 말하고 있습니까? – ashwin19

+0

오른쪽 코드 세그먼트는 주 번들의 데이터로 미리 채워진 plist를 읽습니다. 그러나 수정 된 버전의 파일을 기본 번들에 다시 저장할 수는 없습니다. 문서 디렉토리에 저장해야합니다. 응용 프로그램은 수정해야 할 자원을 응용 프로그램 지원 디렉토리 또는 초기 실행시 docs 디렉토리에 복사하는 것이 일반적입니다. – FluffulousChimp

0

좋아, 여기에 필요한 코드가 있습니다. 나는

static BOOL dicIsNULL = YES; 

.... 

//initialize the variable 
dicIsNULL = YES; 


//READ FILE 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"listNew.plist"]; 

     dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 

     //Check if this dic has values 
     for (id key in dictionary) 
     { 
      //NSLog(@"listNew.plist file has values"); 
      dicIsNULL = NO; 
      break; 
     } 

시작하기 위해 사전에 어떤 정보가 있는지 이제 이미 생성 여부를했던 dictonary에 따라 다음을 참조 기존 아이폰 OS 프로그램에

먼저 확인이 사용하고 있습니다. 사전에 값을 추가하십시오, 이것은 단지 샘플 코드입니다.

if (dicIsNULL == YES) 
     { 
      //NSLog(@"dictionary is NULL"); 

      dictionary = [[NSMutableDictionary alloc] init]; 
      dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:tempArray, infoKeyStr, textTitleField1.text, titleKeyStr, userSelectedCatStr, catKeyStr, nil]; 

     } 

     if (dicIsNULL == NO) 
     { 
      //NSLog(@"dictionary is NOT null"); 

      dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath]; 
      [dictionary setObject: tempArray forKey: infoKeyStr]; 
      [dictionary setObject: textTitleField1.text forKey: titleKeyStr]; 
      [dictionary setObject: userSelectedCatStr forKey: catKeyStr]; 
     } 

희망이 도움이 될 것입니다.

관련 문제