Select to view content in your preferred language

Pushing AGSEnvelope into NSUserDefaults

612
3
Jump to solution
06-28-2012 11:26 AM
PaulLohr
Frequent Contributor
I was trying to push an AGSEnvelope into NSUserDefaults:

         //.h file      @property (nonatomic) IBOutlet AGSMapView *mapView;      @property (nonatomic, strong) AGSEnvelope *mapExtent;     //.m file      NSString *kMapExtent = @"mapExtent";      AGSPolygon *mapExtentPoly = self.mapView.visibleArea;          // see above...self.mapExtent is of type AGSEnvelope     self.mapExtent = mapExtentPoly.envelope;         // this is key to the problem...attempt to convert AGSEnvelope so it can be used as NSData in NSUserDefaults.     self.dataForMapExtent = [NSKeyedArchiver archivedDataWithRootObject:self.mapExtent];    // the program crashes after this line is run       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];          [defaults setObject:self.dataForMapExtent forKey:kMapExtent];          [defaults synchronize];


This is the error message I get when the program crashes:
[AGSEnvelope encodeWithCoder:]: unrecognized selector sent to instance


I think Apple's classes that inherit from NSCoding can convert themselves to NSData. I see that AGSEnvelope inherits from AGSCoding but it looks to only encode/decode JSON.

Do I need to convert the AGSEnvelope to something like an NSDictionary before putting it into NSUserDefaults? Is this the best solution?

Thanks for any help,
Paul Lohr
0 Kudos
1 Solution

Accepted Solutions
DiveshGoyal
Esri Regular Contributor
Paul,

NSUserDefaults can also accept objects of type NSString.

As you noted, most AGS* objects implement AGSCoding protocol. This protocol defines how objects can be converted to and from JSON.
What you will basically need to do is,
1) convert AGSEnvelope to a JSON representation (dictionary), and
2) string-ify the JSON representation.
You can then store this string into NSUserDefaults

You can find more info here. (Refer to the 'ArcGIS classes are JSON friendly section').

View solution in original post

0 Kudos
3 Replies
PaulLohr
Frequent Contributor
I ended up putting AGSEnvelope.xmin, xmax, ymin, and ymax into double data type variables, I then put the doubles into an NSDictionary, then I put the NSDictionary into an NSUserDefaults variable. That's a handful of steps but it works.

In case you missed it or I was unclear, the problem was that AGSEnvelope could not be put into an NSData variable. I wanted to do this because NSData will go directly into an NSUserDefaults variable. This is a little cleaner than going to a double.

Perhaps there is a way to get AGSEnvelope to inherit from NSCoding but I don't know how that would be done. If AGSEnvelope implemented NSCoding's encodeWithEncoder, AGSEnvelope could be put directly into an NSData variable.

If you can help with this, I'd appreciate it.

Paul Lohr
0 Kudos
DiveshGoyal
Esri Regular Contributor
Paul,

NSUserDefaults can also accept objects of type NSString.

As you noted, most AGS* objects implement AGSCoding protocol. This protocol defines how objects can be converted to and from JSON.
What you will basically need to do is,
1) convert AGSEnvelope to a JSON representation (dictionary), and
2) string-ify the JSON representation.
You can then store this string into NSUserDefaults

You can find more info here. (Refer to the 'ArcGIS classes are JSON friendly section').
0 Kudos
PaulLohr
Frequent Contributor
This is good, Divesh. Thank you for taking your time to work this out for me.

I did not realize the usefulness of JSON and the methods in ArcGIS.h for working with JSON.

Much appreciated, sir.

Paul Lohr
0 Kudos