views:

91

answers:

2

I have an NSObject set up to control various elements on my screen.

I initialize the class inside my View Controller like this:

self.pageSetupClass = [[PageSetup alloc] set:self.pageID];

In IB I have added my NSObject object and linked a button to an IBOutlet on it. I've also linked it to an IBAction found in the object. When I click this button the app crashes and I get this error:

2010-09-23 15:33:11.640 BookTest10[49139:207] *** -[NSCFDictionary clickSoundByte:]: unrecognized selector sent to instance 0x4b10bc0
2010-09-23 15:33:11.641 BookTest10[49139:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary clickSoundByte:]: unrecognized selector sent to instance 0x4b10bc0'
2010-09-23 15:33:11.642 BookTest10[49139:207] Stack: (
    42195024,
    43352876,
    42203739,
    41666166,
    41662962,
    2915566,
    3413054,
    3422400,
    3417197,
    3042792,
    2934339,
    2965976,
    51188092,
    41474204,
    41470120,
    51181725,
    51181922,
    2958194
)
terminate called after throwing an instance of 'NSException'

There is nothing in the function at the moment so it must be the calling that crashes it...right?

If you need me to post more code I certainly can. If you have any ideas I'm all ears. Thank You

Edit:

This is the contents of the function set called out when I initialize the PageSetup object:

-(PageSetup*) set:(int) i {
    self = [super init];
    if(self) {
        self.iD=i;

        self.pageSetupFile = [[NSBundle mainBundle] pathForResource:@"PageSetup" ofType:@"plist"];
        self.pageSetupArray = [[NSMutableArray alloc] initWithContentsOfFile:self.pageSetupFile];
        self.pageInfo = [self.pageSetupArray objectAtIndex:self.iD];

        [self initializeSoundBytes];
        [self initializeAnimations];
        [self initializeToys];

        NSLog(@"Page Setup Initializing: %@", self.pageInfo);
        return self;
    }
    return self;
}

Another Edit:

I set my set function to return (void) and no longer initialize it using init.

I now receive this error message:

2010-09-23 17:10:05.096 BookTest10[50519:207] *** -[UITouch clickSoundByte:]: unrecognized selector sent to instance 0x4b1b580
2010-09-23 17:10:05.097 BookTest10[50519:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITouch clickSoundByte:]: unrecognized selector sent to instance 0x4b1b580'
2010-09-23 17:10:05.099 BookTest10[50519:207] Stack: (
    42166352,
    43324204,
    42175067,
    41637494,
    41634290,
    2886894,
    3384382,
    3393728,
    3388525,
    3014120,
    2905667,
    2937304,
    51159420,
    41445532,
    41441448,
    51153053,
    51153250,
    2929522
)
terminate called after throwing an instance of 'NSException'

Final Edit:

I had forgotten to connect my NSObject to the File's Owner in IB.

A: 
self.pageSetupClass = [[PageSetup alloc] set:self.pageID];

What does set: return? Is that a method of your PageSetup class? Does it return self?
Because apparently you're assigning pageSetupClass to whatever set: returns...
Unless set: returns self, you should try this:

self.pageSetupClass = [[PageSetup alloc] init];
[self.pageSetupClass set:self.pageID];

Edit:

ok, if you added the object in IB, the object was already instantiated for you when you loaded the nib, and the IBAction and IBOutlet connections have already been made. When you do self.pageSetupClass = ..., you're setting pageSetupClass to a new instance, and that probably leaves the button with a bad target for its IBAction. If the object was instantiated from the nib, and you have a reference to it in pageSetupClass from a IBOutlet, then just set whatever you want in the object, no need to alloc a new one.

filipe
Yes set returns self.-(PageSetup*) set:(int) i { return self; }
emachine
You should do that even if it returns self. The alloc-init pair is really important in Objective-C.
Chuck
Unfortunately it doesn't change the error message
emachine
ok... I don't really know what's the problem then, but just as a "best practices" suggestion, maybe you should rename your 'set:' method to something like 'initWithInt:'. Sorry for not being able to help you with this.
filipe
That's okay. Always appreciate an attempt. Could it be that when I reinitialize this object I'm breaking the connection with the button on screen?
emachine
oh, I think you got it. Depending on how you added it on IB. Is your PageSetup object in IB the "File's Owner" or did you add an "Object" and set its class to PageSetup?
filipe
Added an Object and set the class to PageSetup
emachine
Ah, thank you. I now have a new (but similar) Error message though. I've reference it as "Another Edit:" in my original post
emachine
Aha! I had yet to link the NSObject to the File's Owner in IB. It seems to be working now. Thank you very much for your patients.
emachine
no problem, glad you figured it out :)
filipe
+1  A: 

Could it be that when I reinitialize this object...

Wait... what? In Objective-C you never call init* methods more than once.

In any case, the underlying problem appears to be a straightforward memory management issue. Something is being released before its time and it just so happens that a dictionary lands at that location. Turn on zombie detection and it'll likely catch the problem.

bbum
Ah, I'll def not do that then. Thanks for the tip. Does that apply even if I deallocate the object and want to make it again? Not sure if that question makes sense.
emachine
Doing something like `[foo release]; foo = [[Bar alloc] init];` is perfectly valid, but **you aren't re-using an object in that case**, but just resetting a reference to point to the new object.
bbum