CLLocationCoordinate2D

CLLocationCoordinate2D

本文介绍了将CLLocationCoordinate2D存储到NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to iphone development, and i want to store CLLocationCoordinate2D object to NSUserDefaults. I am Trying

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

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

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

So How to store CLLocationCoordinate2D into NSUserDefaults?Thanks.

推荐答案

as

pdrcabrod

说默认对象必须是属性列表,即 NSData 的实例, NSString NSNumber NSDate NSArray ,或 NSDictionary

As

pdrcabrod

Said "A default object must be a property list, that is, an instance of NSData, NSString, NSNumber, NSDate, NSArray, or 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"]);

这篇关于将CLLocationCoordinate2D存储到NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 21:41