非同期のNSPropertyListSerialization APIを使用することも、NSDictionaryの同期簡易メソッドを使用することもできます。
チェックアウトWRITETOFILEにNSDictionary Apple Docsでの議論:それはまた
どのように動作するかの詳細は、自動的方法は、This articleは、一般的にココアでシリアル化のいくつかの良い情報を持っています。
次のコードを使用してください。
NSDictionary *dictionaryFromDisk = [NSDictionary dictionaryWithContentsOfFile:backedUpUserDefaultsPath];
それとも、あなたはそれを保存する方法と似てNSPropertyListSerialization、からのストリーム/ NSDataのアプローチを使用することができます。
//Get the user documents directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//Create a path to save the details
NSString *backedUpUserDefaultsPath = [documentsDirectory stringByAppendingPathComponent:@"NSUserDefaultsBackup.plist"];
//Get the standardUserDefaults as an NSDictionary
NSDictionary *userDefaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
//The easiest thing to do here is just write it to a file
[userDefaults writeToFile:backedUpUserDefaultsPath atomically:YES];
//Alternatively, you could use the Asynchronous version
NSData *userDefaultsAsData = [NSKeyedArchiver archivedDataWithRootObject:userDefaults];
//create a property list object
id propertyList = [NSPropertyListSerialization propertyListFromData:userDefaultsAsData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:nil];
//Create and open a stream
NSOutputStream *outputStream = [[NSOutputStream alloc] initToFileAtPath:backedUpUserDefaultsPath append:NO];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
outputStream.delegate = self; //you'll want to close, and potentially dealloc your stream in the delegate callback
[outputStream open];
//write that to the stream!
[NSPropertyListSerialization writePropertyList:propertyList
toStream:outputStream
format:NSPropertyListImmutable
options:NSPropertyListImmutable
error:nil];
あなたは、単にような何かを行うことができ、後方に行きたい
。
NSStringの送受信元はどうですか? – Jules
NSStringを次のようなファイルに書き込むことができます: '' @ '"書き込む文字列:writeToFile:パスアトミック:はいエンコーディング:NSUTF8StringEncodingエラー:nil]'または辞書にキーとして追加し、回答 – Jessedc