将CLLocationCoordinate2D存储到NSUserDefaults

时间:2013-09-20 06:54:16

标签: iphone ios objective-c nsuserdefaults cllocationmanager

我是iphone开发的新手,我想将CLLocationCoordinate2D对象存储到NSUserDefaults。 我正在尝试

    [defaults setObject:location forKey:@"userLocation"];

但是它提供错误'将CLLocationCoordinate2D发送到不兼容的类型ID的参数'

那么如何将CLLocationCoordinate2D存储到NSUserDefaults? 感谢。

6 个答案:

答案 0 :(得分:33)

正如 pdrcabrod 所说,

“默认对象必须是属性列表,即NSDataNSStringNSNumberNSDateNSArray的实例,或者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"]);

答案 1 :(得分:15)

这就是我要做的事情:

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)];

答案 2 :(得分:3)

  

NSUserDefaults类提供了访问常用类型(如浮点数,双精度数,整数,布尔值和URL)的便捷方法。默认对象必须是属性列表,即(或集合的实例组合)的实例:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。 如果要存储任何其他类型的对象,通常应将其存档以创建NSData实例

请参阅:NSUserDefaults

答案 3 :(得分:2)

你可以像这样设置..

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"] ];

我希望它有所帮助。

答案 4 :(得分:1)

正如其他人所说,首先将其转换为DictionaryData对象,然后像往常一样保存它。在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]!)
    }

}

答案 5 :(得分:1)

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)