2011-02-09 12 views
-1

どのように.... iphoneでplistファイルからの読み取りおよび書き込みデータのための別のクラスにNSMutableDictionary

任意のアイデアをNSMutable辞書を使用するには?

答えて

1

あなたはNSDictionary referenceでで見つけることができるように、このクラスは、ファイルinitWithContentsOfFile:(NSString *)pathからNSDictionaryのを作成するための方法があります。

ここで、myDictionaryはplist名です(plistがリソースバンドルの場合)。 あなたはこの方法で、ディスク上のplistを書くことができます。

[dict writeToFile:filePath atomically: YES]; 

filePathには、あなたの先のパスです。

+0

Bermardiさん、お世話になりました。 – donkarai

0

このtutorialのPlistファイルの例を参照してください。

// 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

関連する問題