3

I'm having trouble encoding and saving a list of custom objects containing a MKMapItem to NSUserDefaults.

Firstly, I get the selected MKMapItem from an array of MKMapItems used for a tableView and store that in my sharedManager instance. (All the values in sharedManager will be used later to create a custom object).

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// Get the tapped MKMapItem
MKMapItem *selectedMapItem = self.searchResults[indexPath.row];

// Create a sharedManager instance
MyManager *sharedManager = [MyManager sharedManager];

// Set the workRegion and workLocation in sharedManager
NSLog(@"selectedMapItem: %@", [selectedMapItem name]);
sharedManager.workLocation = selectedMapItem;

// Post a notification to alert the PreviewMapViewController
[[NSNotificationCenter defaultCenter] postNotificationName:@"showAnnotations" object:self.searchResults];
[[NSNotificationCenter defaultCenter] postNotificationName:@"zoomToAnnotation" object:selectedMapItem];
[[NSNotificationCenter defaultCenter] postNotificationName:@"showMap" object:nil];
}

enter image description here enter image description here

This is the code I use to take the MKMapItem from sharedManager and put it in the custom object I've created:

MyManager *sharedManager = [MyManager sharedManager];
newModel.workLocation = sharedManager.workLocation;

My custom object stores workLocation in its header file with a property as follows:

@property (nonatomic, strong) MKMapItem *workLocation;

This is the implementation file where I encode and decode the workLocation object:

@implementation WorkLocationModel

-(id)init {
// Init self
self = [super init];
if (self)
{
    // Setup
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.workLocation forKey:@"workLocation"];
}

-(instancetype)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self)
self.workLocation = [coder decodeObjectForKey:@"workLocation"];
return self;
}

@end

enter image description here

My breakpoint set to catch all the exceptions breaks on the encodeObject line.

The error occurs when I add this custom object to a NSMutableArray and then save that array using:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_myObjects] forKey:@"myObjects"];

Exception: -[MKMapItem encodeWithCoder:]: unrecognized selector sent to instance 0x7f9f14acf400

Can anyone help me with this?

UPDATE:

NSData *workLocationData = [NSKeyedArchiver archivedDataWithRootObject:sharedManager.workLocation];

1 Answer 1

1

MKMapItem does not conform to NSCoding or SSecureCoding.

You will need to encode the individual items and re-create a MKMapItem on decode.

Sign up to request clarification or add additional context in comments.

13 Comments

can I convert it to NSData and if so - how?
as it doesn't conform to NSCoding, how can I encode each individual item?
That is basically what NSCoding does.
so I encode MKMapItem with NSKeyedArchiver and then also encode the whole custom object with NSKeyedArchiver as well?
NSArchiver, a concrete subclass of NSCoder so first you need to implement NSCoder which means encodeWithCoder and initWithCoder. Then you will have an NSData object. This is usually done so the NSData can be saved to a file.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.