【发布时间】:2011-06-29 17:07:44
【问题描述】:
好吧,我知道这听起来可能很简单,但我确实到处寻找,但找不到直接的答案。每次获得更新时,我都会尝试将位置坐标保存到文件中 - 听起来很简单....我有两个问题:一个是数据类型(writeToFile 似乎只保存 NSData),另一个是附加到文件的结尾。我尝试使用 NSKeyedArchiver,但它写了一堆垃圾,我找不到如何用它附加到文件末尾。
这是我的代码 - 如果您能提供帮助,我将不胜感激。谢谢!
....
NSMutableArray *array = [[NSMutableArray alloc] init];
NSNumber *numLat = [NSNumber numberWithFloat:location.coordinate.latitude];
NSNumber *numLong = [NSNumber numberWithFloat:location.coordinate.longitude];
[array addObject:numLat];
[array addObject:numLong];
NSFileHandle *file;
file = [NSFileHandle fileHandleForUpdatingAtPath: @"./location.txt"];
if (file == nil)
NSLog(@"Failed to open file");
[file seekToEndOfFile];
[file writeData: array]; //BTW - this line doesn't work if I replace array with numLat which is an NSNumber - unlike what many people have said in various discussions here
OR - 保存到文件部分(最后两行):
NSString *path = @"./location.txt";
[NSKeyedArchiver archiveRootObject:array toFile:path];
【问题讨论】:
标签: iphone objective-c xcode location core-location