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

  • Home
  • SEARCH
  • 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 1068693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:19:11+00:00 2026-05-16T20:19:11+00:00

I made a class. This is the h file. // MyClass.h #import <Foundation/Foundation.h> @interface

  • 0

I made a class. This is the h file.

//  MyClass.h
#import <Foundation/Foundation.h>


@interface MyClass : NSObject <NSCoding> {
    NSString *string1;
    NSString *string2;

}
@property (nonatomic, retain) NSString *string1;
@property (nonatomic, retain) NSString *string2;

@end

This is the m file

//  MyClass.m
#import "MyClass.h"


@implementation MyClass
@synthesize string1, string2;

- (void)encodeWithCoder:(NSCoder *)coder;
{
    NSLog(@"encodeWithCoder");

        [coder encodeObject:string1 forKey:@"string1"];
        [coder encodeObject:string2 forKey:@"string2"]; 


}

- (id)initWithCoder:(NSCoder *)coder;
{
    NSLog(@"initWithCoder");
    self = [super init];

        self.string1 = [coder decodeObjectForKey:@"string1"];
        self.string2 = [coder decodeObjectForKey:@"string2"];


    return self;
}


- (void)dealloc {

    [string1 release];
    [string2 release];
    [super dealloc];
}


@end

I created an array of these objects like this

MyClass *object1 = [[MyClass alloc] init];
object1.string1 = @"object1 string1";
object1.string2 = @"string1 string2";
MyClass *object2 = [[MyClass alloc] init];
object2.string1 = @"object2 string1";
object2.string2 = @"object2 string2";
theArray = [[NSMutableArray alloc] initWithObjects:object1, object2, nil];

Then I saved the array like this

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:theArray] forKey:@"savedArray"];

Then I loaded the array from disk like this.

NSData *theData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedArray"];
if (theData != nil) {
    NSLog(@"found something");
    theArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];
}

The program runs normally without error until it gets to this

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     detailViewController.myObject = [theArray objectAtIndex:indexPath.row];

It crashes on that last line. It only crashes if I load the array using NSUserDefaults, however I don’t notice anything I did wrong with that part.

When it crashes this is what the debugger says

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5955e50'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02395919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x024e35de objc_exception_throw + 47
    2   CoreFoundation                      0x0239742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x02307116 ___forwarding___ + 966
    4   CoreFoundation                      0x02306cd2 _CF_forwarding_prep_0 + 50
    5   custom object array save test       0x00002872 -[RootViewController tableView:didSelectRowAtIndexPath:] + 156
    6   UIKit                               0x0032b718 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
    7   UIKit                               0x00321ffe -[UITableView _userSelectRowAtIndexPath:] + 219
    8   Foundation                          0x00038cea __NSFireDelayedPerform + 441
    9   CoreFoundation                      0x02376d43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
    10  CoreFoundation                      0x02378384 __CFRunLoopDoTimer + 1364
    11  CoreFoundation                      0x022d4d09 __CFRunLoopRun + 1817
    12  CoreFoundation                      0x022d4280 CFRunLoopRunSpecific + 208
    13  CoreFoundation                      0x022d41a1 CFRunLoopRunInMode + 97
    14  GraphicsServices                    0x02bfa2c8 GSEventRunModal + 217
    15  GraphicsServices                    0x02bfa38d GSEventRun + 115
    16  UIKit                               0x002c7b58 UIApplicationMain + 1160
    17  custom object array save test       0x00002160 main + 102
    18  custom object array save test       0x000020f1 start + 53
    19  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'

Edit:

I was able to fix the problem by replacing this line

theArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];

with this liine

theArray = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];

so I guess the question now is why did that solve the problem?

  • 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-05-16T20:19:11+00:00Added an answer on May 16, 2026 at 8:19 pm

    I don’t see anything wrong with your archiving/unarchiving code. I suspect that indexPath.row is out of range.

    EDIT after OP edited question:

    The error message says: -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x5955e50

    You’ll notice that this means that ‘theArray’ is actually pointing at an NSString. Somewhere you have assigned the wrong thing to your ‘theArray’ variable.

    EDIT again after another OP edit:

    In your original code you had been using alloc/initWithArray: instead of arrayWithArray:.

    The arrayWithArray: usage didn’t work for you because that method returns an autoreleased object. So by the time you used it later that reference could have been pointing at just about anything, and hence in one case it happened to be pointing at a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a class. This is the h file. // MyClass.h #import <Foundation/Foundation.h> @interface
I made a custom class. This is the h file @interface Player : NSObject
When I compile something like this: public class MyClass { void myMethod(String name, String
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I've made a class which inherits from UserControl, but which I only want to
I have a class made up of several fields, and I have several constructors.
I have to use a class/assembly made in C# .NET from native C++ application.
I've made some unit tests (in test class). The tutorial I've read said that
I have a JavaScript class that I have made and put it into its

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.