我是 iphone 开发新手,我想将CLLocationCoordinate2D
对象存储到NSUserDefaults
. 我在尝试
[defaults setObject:location forKey:@"userLocation"];
但它给出了错误'将 CLLocationCoordinate2D 发送到不兼容类型 id 的参数'
那么如何CLLocationCoordinate2D
存入NSUserDefaults
呢?谢谢。
我是 iphone 开发新手,我想将CLLocationCoordinate2D
对象存储到NSUserDefaults
. 我在尝试
[defaults setObject:location forKey:@"userLocation"];
但它给出了错误'将 CLLocationCoordinate2D 发送到不兼容类型 id 的参数'
那么如何CLLocationCoordinate2D
存入NSUserDefaults
呢?谢谢。
正如pdrcabrod所说,
“默认对象必须是属性列表,即 , NSData
, NSString
, NSNumber
, NSDate
,NSArray
或的实例NSDictionary
。”
我用这个来存储,
NSNumber *lat = [NSNumber numberWithDouble:location.latitude];
NSNumber *lon = [NSNumber numberWithDouble:location.longitude];
NSDictionary *userLocation=@{@"lat":lat,@"long":lon};
[[NSUserDefaults standardUserDefaults] setObject:userLocation forKey:@"userLocation"];
[[NSUserDefaults standardUserDefaults] synchronize];
并使用,
NSDictionary *userLoc=[[NSUserDefaults standardUserDefaults] objectForKey:@"userLocation"];
NSLog(@"lat %@",[userLoc objectForKey:@"lat"]);
NSLog(@"long %@",[userLoc objectForKey:@"long"]);
这就是我要做的:
NSData *data = [NSData dataWithBytes:&coordinate length:sizeof(coordinate)];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];
然后你可以像这样检索它:
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
CLLocationCoordinate2D coordinate;
[data getBytes:&coordinate length:sizeof(coordinate)];
NSUserDefaults 类提供方便的方法来访问常见类型,例如浮点数、双精度数、整数、布尔值和 URL。默认对象必须是一个属性列表,即以下实例(或集合实例的组合):NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。如果要存储任何其他类型的对象,通常应该将其归档以创建 NSData 的实例
请参阅:NSUserDefaults
import CoreLocation
// Store an array of CLLocationCoordinate2D
func storeCoordinates(_ coordinates: [CLLocationCoordinate2D]) {
let locations = coordinates.map { coordinate -> CLLocation in
return CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
let archived = NSKeyedArchiver.archivedData(withRootObject: locations)
UserDefaults.standard.set(archived, forKey: "coordinates")
UserDefaults.standard.synchronize()
}
// Return an array of CLLocationCoordinate2D
func loadCoordinates() -> [CLLocationCoordinate2D]? {
guard let archived = UserDefaults.standard.object(forKey: "coordinates") as? Data,
let locations = NSKeyedUnarchiver.unarchiveObject(with: archived) as? [CLLocation] else {
return nil
}
let coordinates = locations.map { location -> CLLocationCoordinate2D in
return location.coordinate
}
return coordinates
}
现在让我们测试一下实现:
let testCoordinates = [
CLLocationCoordinate2D(latitude: 0.123456789, longitude: 0.123456789),
CLLocationCoordinate2D(latitude: 1.123456789, longitude: 1.123456789),
CLLocationCoordinate2D(latitude: 2.123456789, longitude: 2.123456789)
]
self.storeCoordinates(testCoordinates)
let coordinates = loadCoordinates()
print(coordinates)
可以这样设置。。
CLLocationCoordinate2D coordinate; // need to set your coordinate here
[[defaults setObject:[NSString stringWithFormat:@"%g",coordinate.latitude] forKey:@"latitude"] ];
[[defaults setObject:[NSString stringWithFormat:@"%g",coordinate.longitude] forKey:@"longitude"] ];
我希望它有帮助。
正如其他人所说,首先将其转换为Dictionary
orData
对象,然后像往常一样保存它。在 Swift 中,我强烈推荐使用扩展。这是我所做的:
extension CLLocationCoordinate2D {
private static let Lat = "lat"
private static let Lon = "lon"
typealias CLLocationDictionary = [String: CLLocationDegrees]
var asDictionary: CLLocationDictionary {
return [CLLocationCoordinate2D.Lat: self.latitude,
CLLocationCoordinate2D.Lon: self.longitude]
}
init(dict: CLLocationDictionary) {
self.init(latitude: dict[CLLocationCoordinate2D.Lat]!,
longitude: dict[CLLocationCoordinate2D.Lon]!)
}
}