2011-02-09 5 views
-1

아이폰의 plist 파일에서 데이터를 읽고 쓰는 다른 클래스에서 NSMutable Dictionary를 사용하는 방법 ....NSMutableDictionary

어떤 아이디어?

답변

1

,이 클래스는 파일 initWithContentsOfFile:(NSString *)path에서있는 NSDictionary를 제작하는 방법이있다. 다음과 같이 할 수 있습니다 :

여기서 myDictionary는 plist 이름입니다 (plist가 자원 번들의 경우). 당신은 방법으로 디스크에 PLIST를 작성할 수 있습니다

[dict writeToFile:filePath atomically: YES]; 

를 참고 FilePath 목적지의 경로입니다.

+0

Bermardi님께 도움을 주신 .. – donkarai

0

tutorial의 Plist 파일 예제를 참조하십시오. 당신이 NSDictionary reference에서에서 찾을 수 있듯이

// Look in Documents for an existing plist file 
NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
myPlistPath = [documentsDirectory stringByAppendingPathComponent: 
    [NSString stringWithFormat: @"%@.plist", plistName] ]; 
[myPlistPath retain]; 

// If it's not there, copy it from the bundle 
NSFileManager *fileManger = [NSFileManager defaultManager]; 
if (![fileManger fileExistsAtPath:myPlistPath]) { 
    NSString *pathToSettingsInBundle = [[NSBundle mainBundle] 
     pathForResource:plistName ofType:@"plist"]; 
} 


NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectoryPath 
    stringByAppendingPathComponent:@"myApp.plist"]; 
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path]; 


myKey = (int)[[plist valueForKey:@"myKey"] intValue]; 
myKey2 = (bool)[[plist valueForKey:@"myKey2"] boolValue]; 

[plist setValue:myKey forKey:@"myKey"]; 
[plist writeToFile:path atomically:YES]; 
+0

답장을 보내 주셔서 감사합니다 ... – donkarai