2014-02-08 3 views
0

다음과 같은 이유 때문에 약간 혼란 스럽습니다. 누군가가 나를 계몽 할 수 있습니까?NSKeyedArchiver를 사용하여 CLLocation 객체를 저장하는 방법은 무엇입니까?

I filePath 반환 한 기능이 있습니다

// Store this location so it can persist: 
BOOL success = [NSKeyedArchiver archiveRootObject:_startLocation toFile:[self lastLocationPersistenceFilePath]]; 
if (!success) { 
    NSLog(@"Could not persist location for some reason!"); 
} else { 
    NSLog(@"New location has been stored."); 
} 

을 내 viewDidLoad:에서 : 버튼에 의해 트리거하는 방법으로, 그리고

- (NSString *) lastLocationPersistenceFilePath { 
    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"last_location"]; 
    return filePath; 
} 

, 나는 그래서 같은 CLLocation 객체를 저장하려고 로드하려고 시도했습니다 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Retrieve last location: 
    CLLocation *decodedLocation = [NSKeyedUnarchiver unarchiveObjectWithFile:[self lastLocationPersistenceFilePath]]; 

    if (decodedLocation) { 
     _startLocation = decodedLocation; 
    } else { 
     NSLog(@"Decoding failed."); 
    } 
} 

물론 어떤 것도 디코딩 할 수 없다는 것을 의미합니다. 나는 명백한/사소한 것 ... 어떤 지시 사항을 놓치고있는 것처럼 느낍니다.

답변

3

단순히 앱의 리소스 디렉토리에 쓸 수 없습니다. 대신 앱의 문서 디렉토리에 씁니다.

코드 예제 "

- (NSString *) lastLocationPersistenceFilePath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [paths firstObject]; 
    NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"last_location"]; 
    return filePath; 
} 
+0

답장을 보내 주셔서 감사 많은. 내가 당신과 내 방법을 대체했지만 여전히 객체를 저장하지 않습니다. 난 그냥 '말 대신에 더 도움이 정보를 제공 할 수 있으면 좋겠다 작동하지 않습니다. "내가 시도하거나 점검 할 수있는 것은 무엇입니까? – Florian

+0

[이 해결책] (http://stackoverflow.com/questions/1813166/persisting-corelocation)을 수행하여 작동시킬 수있었습니다. , NSUserDefaults를 사용하여 위도와 경도를 double 값으로 저장했습니다. – Florian