2
NSCodingを使用して、アプリケーションからいくつかの値を保存しようとしています。私は値を保存することができますが、それを取得することはできません。私は、プロトコルを宣言していますのはここNSCoding NSKeyedUnarchiver unarchiveObjectWithFile:nullを返す
です:私は、プロトコルに準拠するよどこ
@interface AddReminderEventViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, NSCoding>
はここにあります:
-(void)encodeWithCoder:(NSCoder *)enCoder
{
[enCoder encodeObject:self.eventRepeatDurationDate forKey:kEventRepeatDuration];
[enCoder encodeObject:self.eventIDsMutableArray forKey:kEventIDsMutableArray];
[enCoder encodeObject:self.eventRepeatDurationString forKey:@"mytest"];}
、ここで:どこ
-(id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]){
self.eventRepeatDurationDate = [[decoder decodeObjectForKey:kEventRepeatDuration] retain];
self.eventIDsMutableArray = [[decoder decodeObjectForKey:kEventIDsMutableArray] retain];
self.eventRepeatDurationString = [[decoder decodeObjectForKey:@"mytest"] retain];} return self; }
とここですアーカイブとアーカイブ解除を行うメソッドを呼び出します。
[self saveDataToDisk];
[self loadDataFromDisk];
、ここで、これらの方法の遺体があり、それがのNSLogの内容です:
- (void)saveDataToDisk {
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";
//reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH1: reminderEventIDsPathString is %@", reminderEventIDsPathString);
NSMutableDictionary *rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue:eventRepeatDurationString forKey:@"mytest"];
NSLog(@"1rootObject IS %@", rootObject);
[NSKeyedArchiver archiveRootObject:rootObject toFile:reminderEventIDsPathString];}
reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive
2012-01-16 15:47:48.578 [29658:15503] 1rootObject IS {mytest = 7;}
、ここではそののNSLog内容に沿ってunarchiverコードです:
- (void)loadDataFromDisk {
NSString *testValue = [[NSString alloc] init];
NSString *reminderEventIDsPathString = @"~/Library/Application Support/ReminderIDs.archive";
reminderEventIDsPathString = [reminderEventIDsPathString stringByExpandingTildeInPath];
NSLog(@"WATCH2: reminderEventIDsPathString is %@", reminderEventIDsPathString);
NSMutableDictionary *rootObject;
rootObject = [[NSKeyedUnarchiver unarchiveObjectWithFile:reminderEventIDsPathString] retain];
NSLog(@"2rootObject IS %@", rootObject);
NSLog(@"WATCH3 - %@", [rootObject objectForKey:@"mytest" ]);
if ([rootObject valueForKey:@"mytest"]) {
testValue = [rootObject valueForKey:@"mytest"];
NSLog(@"WATCH: testValue is %@", testValue); } }
2012-01-16 15:48:14.965 [29658:15503] WATCH2: reminderEventIDsPathString is /Users/tester/Library/Application Support/iPhone Simulator/5.0/Applications/E26D57DE-C4E1-4318-AEDD-7207F41010A9/Library/Application Support/ReminderIDs.archive
2012-01-16 15:48:17.879 [29658:15503] 2rootObject IS (null)
私は何をしないのですそのI内容を解凍できませんでしたか?私はちょうどそれをテストするために私のエンコーダ/デコーダのメソッドで最も簡単な値に焦点を当てていますが、文字列の値を取得することさえできません。
おかげ
「間違っている」ということを教えていただけますか?私は、私が図書館の道に書くことになっていたと思った。それは間違っていますか?また、そのパスのファイルが表示され、その中に内容があります。ありがとう – Jazzmine
さて、それは働いた!どうもありがとうございましたが、それがなぜ機能するのかを明確にしていただきありがとうございます。私が持っていた道に書かれた/読んでいないはずのアプリですか?答える時間をとってくれてありがとう。 – Jazzmine
また、あるView Controllerで値を書き出し、別のView Controllerで値を取得する場合は、appDelegateを使用して/参照して、コードの重複を避けるために書き込みと読み取りを行う必要がありますか?再度、感謝します。 – Jazzmine