Re: Mapkit annotation arrays and writing to a file
Re: Mapkit annotation arrays and writing to a file
- Subject: Re: Mapkit annotation arrays and writing to a file
- From: Alex Zavatone <email@hidden>
- Date: Tue, 08 May 2012 18:36:26 -0400
On May 8, 2012, at 5:53 PM, Conrad Shultz wrote:
> On 5/8/12 2:35 PM, Alex Zavatone wrote:
>> Well, I had a similar problem saving the information to a JSON file
>> and had to rebuild the dictionary without the CLLocationCoordinate2D
>> struct, before NSJSONSerialization would serialize the dictionary.
>>
>> The reason I think it's a struct is that's how it's defined in
>> CLLocation.h and right now in the debugger, annotation is showing as
>> (struct objc_object * const), which sorta conflicts with what you
>> wrote (TTBOMK).
>>
>> Here are the details from CLLocation.h:
>
> No one disputes that CLLocationCoordinate2D is a struct. What Jens is
> saying is that you can't store an unwrapped struct in a Cocoa collection
> - be it a dictionary, array, or set.
>
> What's getting lost in translation here is that -annotations is an array
> of objects of type id <MKAnnotation>.
>
> What sort of object conforming to MKAnnotation are you adding to your
> annotations array? Whatever it is, it must have a -coordinate property.
> It is *this* property this is a CLLocationCoordinate2D struct. This is
> how the data is getting stored in a Cocoa collection.
>
> In any event, I don't see how you could directly serialize your
> annotation object to a JSON object for exactly the reason Jens described
> (i.e. type incompatibility).
>
> If you need to convert to JSON you could, for example, write an
> NSValueTransformer that converts your id <MKAnnotation> to an
> NSDictionary that is compliant with NSJSONSerialization requirements.
It looks like my mentioning JSON serialization added to the lack of clarity of my email, but it is another issue entirely. I just used it as a reference problem since I ran into this issue with structs before causing JSONSerialization to barf.
In this case, I was/am trying to write out an MapKitView's annotation array as an array or a dict with writeToFile to create a pList, and at some point, read it back in to restore the annotationList.
I was under the tragically misinformed understanding that since a mapKitView's annotations array was already an array, that I could either write it out to a file directly (on iOS) or declare a key, add it to a dictionary and write it out without doing much of anything special.
When I add my annotations to the mapView's annotations, I'm doing it like so:
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:title address:address coordinate:coordinate];
[self.mapView addAnnotation:annotation];
MyLocation conforms to <MKAnnotation> and is where I have coordinate defined as type CLLocationCoordinate2D.
I'll just walk the annotations array as before, and create another array or dict that will write out a valid dict or array.
Then I'll go over the CoreData samples which seem to address this, or as you suggested, learn the NSTransform bits.
Thanks both of you.
- Alex
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden