Store CLLocationCoordinate2D to NSUserDefaults

cllocationcoordinate2d swift
cllocationcoordinate2d array objective-c
create cllocationcoordinate2d swift 4

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

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

But it gives error 'Sending CLLocationCoordinate2D to parameter of incompatible type id'

So How to store CLLocationCoordinate2D into NSUserDefaults? Thanks.

As pdrcabrod said,

"A default object must be a property list, that is, an instance of NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary."

I use this to store,

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

And access using,

    NSDictionary *userLoc=[[NSUserDefaults standardUserDefaults] objectForKey:@"userLocation"];
    NSLog(@"lat %@",[userLoc objectForKey:@"lat"]);
    NSLog(@"long %@",[userLoc objectForKey:@"long"]);

how to store an array of type cllocationcoordinate2d in , 2 Answers. You cannot store an array of CLLocationCoordinate2D to UserDefaults directly. As the following excerpt of the UserDefaults describes, you cannot directly store an array [CLLocationCoordinate2D] to UserDefaults. NSKeyedArchiver does not work for CLLocationCoordinate2D. You cannot store an array of CLLocationCoordinate2D to UserDefaults directly As the following excerpt of the UserDefaults describes, you cannot directly store an array [CLLocationCoordinate2D] to UserDefaults. Allowed are only combinations of [Int], [String] etc.

This is what i would do:

NSData *data = [NSData dataWithBytes:&coordinate length:sizeof(coordinate)];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];

Then you can retrieve it like this:

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"key"];
CLLocationCoordinate2D coordinate;
[data getBytes:&coordinate length:sizeof(coordinate)];

Store CLLocation into UserDefaults · GitHub, Store CLLocation into UserDefaults. GitHub Gist: instantly share code, notes, and snippets. It is telling me that the value of type String? to a value of type CLLocationCoordinate2D. So how do I convert the CLLocationCoordinate2D coordinates into a value of type String? Answer: This way you can store Locations to NSUserDefaults:

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData

See: NSUserDefaults

Is it possible to save a CLLocation to NSUserDefaults?, Can NSUserDefaults accomplish something like this? Or would I have Do you really need to store a CLLocation or just the latitude and longitude. You can pull  The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s defaults database.

You can set like this..

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

i Hope it helps.

Store CLLocationCoordinate2D in NSUserDefaults, how. pdrcabrod. The specified "Object by default should be a list of properties, that is, an instance of NSData , NSString , NSNumber , NSDate , NSArray or  The NSUserDefaults class provides a way for iOS Apps and Extensions to programmatically interact with the system-wide Defaults System. By using the Defaults System, the user can configure an app's behavior or styling to meet their preferences (based on the design of the app).

As others have said, first convert it into a Dictionary or Data object and then save it as you normally would. In Swift I highly recommend using extensions. Here's what I did:

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

}

Converting CLLocationCoordinate2D to a String that can - iOS, This way you can store Locations to NSUserDefaults: //First Convert it to NSNumber. let lat : NSNumber = NSNumber(double: Location.latitude) let lng  You can always retrieve the most recently collected location from the location property of your CLLocation Manager object. You may create location objects yourself when you want to cache custom location data or calculate the distance between two geographical coordinates. Use CLLocation objects as is and do not subclass them.

Converting CLLocationCoordinate2D to a String that can , So how do I convert the CLLocationCoordinate2D coordinates into a value of type String ? Solution. This way you can store Locations to NSUserDefaults : The UserDefaults object, formerly known as NSUserDefaults, is exceptionally useful for storing small pieces of data in your app. You use it to save your app user’s settings, set some “flags”, or simply use it as a tiny data store. In this article you’ll learn: How to set and get data with UserDefaults

converting cllocationcoordinate2d to a string , I found the answer to this question here: Store CLLocationCoordinate2D to NSUserDefaults. And the co. The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s defaults database.

I found the answer to this question here: Store CLLocationCoordinate2D to NSUserDefaults. And the code is written in Objective C. I understand, for the most​  xamarin ios nsuserdefaults example (3) -180 definitely seems suspicious since it is not 0 or MAX double and seemingly related to coordinates. Perhaps you are grabbing the user's location before the location manager has time to locate them.

Comments
  • should be NSNumber *lat = [NSNumber numberWithDouble:location.coordinate.latitude]; NSNumber *lon = [NSNumber numberWithDouble:location.coordinate.longitude];
  • Thanks for the answer! Be careful using this to store CLLocation object, you might get "EXC_BAD_ACESS code = 1" errors associated with converting a Core Foundation pointer to a Objective-C pointer, which you will later have trouble using because in some instances the object it points to will already be released. See this post... stackoverflow.com/a/27848617/4018041
  • FormigaNinja, you're being sarcastic, right? If the size of the structure will change the data may not be restored properly afterwards.
  • thanks for reply, but now how to archive it to create an instance of NSData to store in NSUSERDEFAULTS.