我正在尝试保存我的CLLocation位置以供将来在我的应用中使用。我想出了以下内容:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/Locations.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[data setValue:[NSKeyedArchiver archivedDataWithRootObject:location] forKey:@"LocationList"];
[data writeToFile:path atomically:YES];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData: (NSData *)[data objectForKey:@"LocationList"]]];
NSLog(@"%@",array);
当我运行我的应用程序时,编译器返回
-[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
我做错了什么?或者有更好的标准方法来保存CLLocation数据吗?
答案 0 :(得分:0)
您已经忘记了您的方法实际返回的内容,当您在对方内部嵌入多个消息调用时会有风险。
您存储了一个CLLocation,因此返回一个CLLocationObject:
[NSKeyedUnarchiver unarchiveObjectWithData: (NSData *)[data objectForKey:@"LocationList"]]
您的代码正在将其视为返回数组:
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:... // Your returned CLLocation here.
这就是数组为NULL的原因。至于如何存储CLLocation数据,编码方法是正确的。 (见this answer for a clear reference to storing CLLocations as NSData objects。)