簡體   English   中英

將CLLocationCoordinate2D存儲到NSUserDefaults

[英]Store CLLocationCoordinate2D to NSUserDefaults

我是iphone開發的新手,我想將CLLocationCoordinate2D對象存儲到NSUserDefaults 我在嘗試

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

但它給出錯誤'將CLLocationCoordinate2D發送到不兼容類型id的參數'

那么如何將CLLocationCoordinate2D存儲到NSUserDefaults 謝謝。

正如pdrcabrod所說,

“默認對象必須是屬性列表,即NSDataNSStringNSNumberNSDateNSArrayNSDictionary的實例。”

我用它來存儲,

    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

你可以像這樣設置..

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

我希望它有所幫助。

正如其他人所說,首先將其轉換為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]!)
    }

}
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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM