2012-01-16 2 views
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; } 

을 여기 어디 I 보관 및 보관 처리 방법을 호출하십시오.

[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) 

내가 놓치고 무엇 내가 그 내용을 보관 취소 할 수 없습니까? 필자는 단지 인코더/디코더 메서드의 값 중 가장 쉬운 값을 테스트하는 데 초점을 맞추고 있지만 문자열 값을 가져올 수는 없습니다.

감사

답변

1

저장하고 알림이 잘못로드 경로. 아마도 이것으로 바꿀 수도 있습니다

NSString *reminderEventIDsPathString = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ReminderIDs.archive"]; 
+0

당신이 '틀린'이라는 뜻을 말해 줄 수 있습니까? 나는 도서관 경로에 글을 써야한다고 생각했다. 그게 잘못된거야? 또한, 그 경로에서 파일을 볼 수 있으며 그 안에 내용이 있습니다. 감사합니다 – Jazzmine

+0

음, 그 일했습니다! 고맙습니다.하지만 왜 그렇게 효과가 있었는지 명확히 해 주시면 감사하겠습니다. 내가 가지고있는 경로에서 앱을 쓰거나 읽지 않아야합니까? 대답 할 시간을내어 주셔서 감사합니다. – Jazzmine

+0

또한, 하나의보기 컨트롤러에 값을 쓰고 다른 컨트롤러에서 값을 가져 오려면 appDelegate를 사용하여 코드의 중복을 피하기 위해 쓰기 및 읽기를 사용해야합니까? 다시 한번 감사드립니다. – Jazzmine