Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:22:30+00:00 2026-06-05T14:22:30+00:00

I have been searching top and bottom for info on how to do this.

  • 0

I have been searching top and bottom for info on how to do this. I landed on a great tutorial! So I am still quite newbie to this. Basically I have been trying to store Map View annotations to an array. The annotations are a separate class which basically overrides / acts as a MKAnnotation for a pin annotation. It has three properties:

  1. Annotation Coordinate
  2. Annotation Title
  3. Annotation Subtitle

This array needs to store into the NSUserDefaults. I faced a problem, here is the log:

[UIMutableIndexPath setObject:forKey:]: unrecognized selector sent to
instance 0x1187b0

My Annotation class objects stored inside the array could not be saved to the user defaults. So I had to turn this array into NSData and then save it, right?

I have a lot of code setup, just not working. Here is how I attempt all of this:

View Controller Class.m:

- (void)syncMap { // this method is called in viewWillDissapear (for running tests) and applicationDidEnterBackground in App Delegate

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject: localOverlays]; // this is the array I was talking about

[defaults setObject:data forKey:@"overlays"];

[defaults synchronize];

}

- (void)initCircles { // called in AppDelegate UIApplicationDelegate method: applicationDidFinishLaunchingWithOptions

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSData *data = [defaults objectForKey: @"overlays"];

    localOverlays = [NSKeyedUnarchiver unarchiveObjectWithData: data];

    if (!localOverlays) {

        // Either there is a problem or it is the first time opening the app

        localOverlays = [[NSMutableArray alloc] init];
    }

}

NOTE: I AM TESTING WITH TWO ANNOTATIONS IN THE ARRAY (localOverlays)

So, my localOverlays can be encoded / archived(using NSCoder) since it is an NSArray. However, I had to add some further setup in my Annotation class. In its .h it uses to NSCoding and MKAnnotation: like the following< NSCoding, MKAnnotation>. Sorry if I am not using the correct term. Here is my .m:

  - (void)encodeWithCoder:(NSCoder *)aCoder { // should only be called when app enters background state, but since that cannot log in the console, like before I set it up so it should also be called in viewWillDissapear

    NSLog(@"encodeCoder called in Annotation"); // gets called twice when the view will disappear... GOOD!

    [aCoder encodeObject: title forKey: @"title"];
    [aCoder encodeObject: subtitle forKey: @"subtitle"];
    [aCoder encodeDouble: coordinate.latitude forKey: @"latitude"];
    [aCoder encodeDouble: coordinate.longitude forKey: @"longitude"];
}


- (id)initWithCoder:(NSCoder *)aDecoder { // Should be called only at startup of app (not the first time you startup the app though... because data will be NULL)

    NSLog(@"In the Annotation class, initWithCoder is called"); // Does get called at appropriate times twice... NICE!

    self = [super init];

    if (self) {

        title = [aDecoder decodeObjectForKey: @"title"];
        subtitle = [aDecoder decodeObjectForKey: @"subtitle"];

        double lat = [aDecoder decodeDoubleForKey: @"latitude"];
        double lon = [aDecoder decodeDoubleForKey: @"longitude"];

        coordinate = CLLocationCoordinate2DMake(lat, lon);
    }

    return self;
}

So as you can see, I have everything setup for archiving, right? Well it seems not… because now in the .m of the ViewController, I also have this code in viewDidLoad:

    for (Annotation *pin in localOverlays) {

    if (pin) {

        NSLog(@"valid pin: _CMD updateCircles");

        [mapView addAnnotation: pin];

    }

}

This code works nicely and fine the first time I open my app and add the pins. Okay, so now I have exited the view and quit the app, and deleted from multitasking bar. When I open it back up, I get a crash at the fast enumeration line:

 -[NSURL countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xcd31140

So, there is something wrong with all my archiving and encoding setup. What is wrong here… I know this was a lengthy question but I tried to structure it well. Am I setting up my code totally incorrect, is there a typo/bug in my code. Thanks all!

UPDATE:

I have got the coordinate variable encoded, therefore when I startup the app after the pin appear at the right coordinate, but when I try to press it to see the title and subtitle, I get the following crash:

 objc_msgSend

So something is released right… just a guess… bad memory management? What would cause this crash in my code?

UPDATE:

I have looked deeper and further into my code and changed a few release statements around, just improved my memory management and done a bit of optimization. Now I get a more specific crash:

*** -[CFString length]: message sent to deallocated instance 0x147490

So my title or subtitle IS deallocated… why? I have checked my code it should be absolutely fine, especially since the coordinates are fine…

UPDATE:

I have solved this problem! I came to the realization, the coordinate two variables, latitude and longitude are doubles, data types… NOT objects! Therefore they are sticking on and working only BECAUSE they are copied… unlike objects which are references. Long story short I needed to retain. Just like this:

        title = [[aDecoder decodeObjectForKey: @"titler"] retain];
    subtitle = [[aDecoder decodeObjectForKey: @"subtitler"] retain];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:22:31+00:00Added an answer on June 5, 2026 at 2:22 pm

    I have solved this problem! I came to the realization, the coordinate two variables, latitude and longitude are doubles, data types… NOT objects! Therefore they are sticking on and working only BECAUSE they are copied… unlike objects which are references. Long story short I needed to retain. Just like this:

            title = [[aDecoder decodeObjectForKey: @"titler"] retain];
            subtitle = [[aDecoder decodeObjectForKey: @"subtitler"] retain];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching for a solution to this and so far found nothing
I have been searching for quite a bit now but I have never been
I have been searching for an answer and trying out stuff for days now
I have been searching around for an answer to this but I can't seem
I have been searching around for a solution for quite some time, hoping someone
I have been searching for a little while on this one. I need a
I have been searching for an existing question for this one, but i am
Been searching everywhere and can't find a way to do this. I have a
I have been working on this for the past couple of hours, and searching
I've been searching around for days trying to find the answer to this, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.