2012-04-04 6 views
0

현재이 plist에 대한 컨트롤러 클래스를 만들고 있습니다.이 plist에는 여러 가지 유형이있는 루트 사전이 있습니다 (숫자, 문자열 및 사전). 내 컨트롤러 클래스에서 plist를 확인한 다음 문서에 추가하여 읽고 읽고 쓸 수 있습니다.사전에 속성 목록의 사전에 값을 추가하는 방법

여기에서 필자는 현재 plist에서 whats를 읽고이 값들을이 클래스에서 설정 한 tempvars로 전달합니다.

내 읽기 방법은 내 plist 컨트롤러 클래스에서와 같습니다.

-(void) readPlistData 
{ 
    // Data.plist code 
    // get paths from root direcory 
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"]; 

    // check to see if Data.plist exists in documents 
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
    { 
     // if not in documents, get property list from main bundle 
     plistPath = [[NSBundle mainBundle] pathForResource:@"EngineProperties" ofType:@"plist"]; 
    } 

    // read property list into memory as an NSData object 
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    // convert static property liost into dictionary object 
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
    if (!temp) 
    { 
     NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
    } 
    // assign values 
    self.protocolSignature = [temp objectForKey:@"Protocol"]; 
    self.requestNumber = [temp objectForKey:@"RequestNumber"]; 

    //How do I add the dictionary values here? 
} 

내가 변수에 데이터를 넣어 이유 때문에 나는 올바른 요청 번호를 수신하고 같은 것들을 확인하고 .. 내 DB에 대해 수행 할 검사에 대해 테스트 할이 값을 사용하기 위하여려고하고 후자

UPDATE : 내 생각에 루트 사전에 사전에 추가하면됩니다. 나는 가까이에 있지 않다고 생각하지만, 내가하는 일에 대해 당신에게 더 나은 단서를 줄 수 있습니다.

self.cacheValue = [temp objectForKey:@"Cache Value"]; 
self.manufacturers = [cacheValue objectForKey:@"Manufacturers"]; 
    self.models = [cacheValue objectForKey:@"Model"]; 
    self.subModels = [cacheValue objectForKey:@"SubModels"]; 

어떤 도움을 주시면 대단히 감사하겠습니다.

enter image description here

+1

plist의 기본 예제를 제공 할 수 있습니까? 나는 당신의 모범에서 그것을 따르는 것이 아닙니다. NSMutableDictionary를 사용하려는 것 같습니다. – rwyland

+0

캐시 값 사전과 해당 하위 값을 코딩하는 방법을 알아 내려고 위와 같이 위와 같이 내 plist 스크린 샷을 업데이트했습니다. –

답변

1

난 당신이 다음을 수행 할 생각 :

가 변경 가능한 사전으로 .H에 cacheValue 속성을 정의합니다.

NSMutableDictionary *cacheValue; 

는 NSMutableDictionary로 plistXml를 직렬화 : 모든 변경 가능한

// This is the root Dictionary 
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization propertyListWithData:plistXML options:NSPropertyListMutableContainersAndLeaves format:NSPropertyListXMLFormat_v1_0 error:&error]; 

때문에, 당신은 지금, 업데이트, 삽입을 읽을 사전 또는 subcontents의 일부를 삭제할 수 있습니다. 예를 들어, 변경 가능한 사전 "캐시 값을"잡아 단지입니다 :

self.cacheValue = [temp objectForKey:@"Cache Value"]; 

개체가 키에 대한 값이없는 경우 전무되지 않았는지 확인해야합니다. plier에 나타나는 키는 이어야합니다 (정확히). 변경이 용이 한 사전에 값을 업데이트

은 간단합니다 :이 도움이

/* 
The flag "atomically" specifies whether the file should be written atomically or not. 
If flag is YES, the receiver is written to an auxiliary file, and then the auxiliary file is renamed to path. 
If flag is NO, the dictionary is written directly to path. 
The YES option guarantees that path will not be corrupted even if the system crashes during writing. 
*/ 
[self.temp writeToFile:plistPath atomically:YES]; 

희망, 환호 : 다시 PLIST에 루트 변경 가능한 사전에 변경 사항을 저장하기 위해 마지막으로

[self.cache setValue:@"New Value" forKey:@"Sub"]; 

그리고, !

+0

괜찮습니다. 지금 솔루션을 구현하려고합니다. 나는 내가 어떻게 가는지 알려 줄 것이다. –

+0

'NSPropertyListMutableContainersAndLeaves'에 대해 가르쳐 주신 것에 감사드립니다. –

관련 문제