2
我想从我的应用程序使用NSCoding保存一些值。我可以保存该值,但无法检索它。NSCoding NSKeyedUnarchiver unarchiveObjectWithFile:返回null
这里就是我声明的协议:
-(void)encodeWithCoder:(NSCoder *)enCoder
{
[enCoder encodeObject:self.eventRepeatDurationDate forKey:kEventRepeatDuration];
[enCoder encodeObject:self.eventIDsMutableArray forKey:kEventIDsMutableArray];
[enCoder encodeObject:self.eventRepeatDurationString forKey:@"mytest"];}
这里:
@interface AddReminderEventViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, NSCoding>
这里是我在遵守该协议的其中
-(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内容一起的取档代码:
- (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)
我在想什么,我无法解压内容?我只关注我的编码器/解码器方法中最简单的值来测试它,但我甚至无法获得字符串值的工作。
谢谢
你能告诉我你的意思是'错误'吗?我以为我应该写信给图书馆路径。那是不正确的?另外,我在该路径上看到该文件,并在其中包含内容。谢谢 – Jazzmine 2012-01-16 22:33:56
那么,工作!非常感谢你,但是我也希望你能澄清为什么这会起作用。应用程序不应该写入/读取我拥有的路径吗?感谢您花时间回答。 – Jazzmine 2012-01-16 22:41:53
此外,如果我想将值写入一个视图控制器并在另一个视图控制器中检索它,我是否应该使用/引用appDelegate执行写入和读取以避免重复代码?再次感谢。 – Jazzmine 2012-01-16 22:45:56