I'm attempting to initialize an array and add a chapter object to an array of book objects, but it crashes with the error:
2012-07-25 21:41:01.503 Project1[2364:f803] -[__NSCFString
arrayOfChapters]: unrecognized selector sent to instance 0x6ad80b0
2012-07-25 21:41:01.505 Project1[2364:f803] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString arrayOfChapters]: unrecognized selector sent to
instance 0x6ad80b0'
My code:
Chapter *myChapter = [[Chapter alloc]init];
myChapter.pageCount = self.TextField2.text;
myChapter.chapterTitle = self.TextField1.text;
if(!currentBook.arrayOfChapters)
{
currentBook.arrayOfChapters = [[NSMutableArray alloc]init];
}
currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
[currentBook.arrayOfChapters addObject:myChapter];
I think the code is correct, is there something set up wrong with my project? I believe it's the initialization which is causing the actual crash, but there isn't anything non-standard there.
you can make a breakpoint on the line "if(!currentBook.arrayOfChapters)" to check whether the currentBook is nil;
make a breakpoint on the line"currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
" to check whether the segControl.selectedSegmentIndex >= [books count]
It looks like currentBook is a string or not initialized.
I may be misreading this but...
if(!currentBook.arrayOfChapters)
{
// creating array for current book
currentBook.arrayOfChapters = [[NSMutableArray alloc]init];
}
// reassigning a new book to current book. Are you sure this new one has array of chapters?
currentBook = [books objectAtIndex:segControl.selectedSegmentIndex];
// trying to access array of chapters
[currentBook.arrayOfChapters addObject:myChapter];
Shouldn't you be assigning first and then creating array if it's not already there?
Related
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [LeavesCache setDataSource:]: unrecognized selector sent to instance 0x7db1f30
Added Exception breakpoint and found that problem is at this line
pageCache = [[LeavesCache alloc] initWithPageSize:self.bounds.size];
- (void) initialize {
backgroundRendering = NO;
pageCache = [[LeavesCache alloc] initWithPageSize:self.bounds.size];
}
- (id) initWithPageSize:(CGSize)aPageSize
{
if (self = [super init]) {
pageSize = aPageSize;
pageCache = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void) setDataSource:(id<LeavesViewDataSource>)value {
pageCache.dataSource = value;
}
Have no idea how to fix this if some one can help me in this
I'm assuming you're using [this library][1], but it doesn't match up with what you've posted. Because the LeavesCache library on GitHub has no setDataSource method in the code - it's property declared instead. Have you made modifications to the source? Somebody has, because you seem to be setting the pageCache instance variable in one method to a NSMutableDictionary, and in another to a LeavesCache object.
Is there any particular reason why you're using this library? As far as I can tell, it hasn't been updated for three years, and iOS has supported iBooks like page turning interface since iOS 5 natively, using the UIPageViewController class.
I have a strange problem (strange if you ask me). Im using a NSNumber object to store a number (doh). When I try to "modify" it it crashes my application. The code I'm using looks like this:
if ([frequency intValue] > 19999)
return;
frequency = [NSNumber numberWithInt:([frequency intValue] + 1)]; //I think this line is causing me the problem
[freqLabel setText:[NSString stringWithFormat:#"%i Hz", [frequency intValue]]];
Where is frequency the NSNumber and freqLabel my label to which I write the value every time this gets called.
Why is this incorrect? It works when I call it for the first time. Is it that NSNumber numberWithInt always returns a new object which I'm trying to assign to frequency?
How do I fix this? Whats the correct way to change a NSNumber's value?
Sorry for my bad english (if there are any mistakes).
EDIT:
The error log looks like this:
[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0
2012-05-09 16:39:28.064 MyApp[31939:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType intValue]: unrecognized selector sent to instance 0x73430e0'
*** First throw call stack:
(0x17a6022 0x28afcd6 0x17a7cbd 0x170ced0 0x170ccb2 0x4821 0x17a7e99 0x49d14e 0x49d0e6 0x543ade 0x543fa7 0x543266 0x4c23c0 0x4c25e6 0x4a8dc4 0x49c634 0x2e49ef5 0x177a195 0x16deff2 0x16dd8da 0x16dcd84 0x16dcc9b 0x2e487d8 0x2e4888a 0x49a626 0x1cca 0x1c15)
terminate called throwing an exception
But it doesn't always show this error. Sometimes it causes an EXC_BAD_ACCESS. Should I store my variable in a temporary NSNumber?
You can't just alloc/init something once and then you have a lifelong reference to that type. When you assign frequency to numberWithInt, then you are overwriting the previous alloc/init value with an autorelease value (which will be released later and cause the exact behavior you are describing). The reason it works with self.frequency is because your property is set as a retain property, so it automatically retains the autorelease value. Add a retain to your numberWithInt line and it will be fine (or do what you are doing now with self.frequency).
I would try doing this instead
int myNumber = ([frequency intValue] + 1);
frequency = [NSNumber numberWithInt:myNumber];
Yes, numberWithInt: does indeed return a new object. You're probably not retaining this object. Just properly retain frequency when assigning it a new NSNumber. Without context, I'm not sure the best way to accomplish this, but one way is to make frequency a property of your object and using the accessor method.
It would seem that you've already initialized and assigned some value prior to NSNumber, like you have it inside an array for example.
Basically NSNumber objects are immutable, so changing their value is not possible anyway.
You can do it this workaround if you use it inside an array:
NSMutableArray *myOldArray = [[NSMutableArray alloc] init];
myOldInt = 3;
myOldArray[4] = [NSNumber numberWithInt:myOldInt]; // for example
NSMutableArray *myNewArray = [[NSMutableArray alloc] init];
myInt = myOldInt+2;
NSMutableArray *row = [NSMutableArray arrayWithObjects:myOldArray[1],myOldArray[2],myOldArray[3],[NSNumber numberWithInt:myInt],[NSNumber numberWithInt:myInt2],nil];
[myNewArray addObject:row];
I am trying to implement a singleton, it is acting as a stub DAO and I need different areas of the application to be able to read and write to it. The first class that uses it can do so without any issue using my sharedSingleton class level constructor, however when I attempt to access this from another class in the exact same way I get a EXC_BAD_ACCESS error and the debug line in the 1st line of the method I am calling on the singleton is never hit.
+(DAOController *) sharedSingleton
{
static DAOController *sharedSingleton;
#synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[DAOController alloc] init];
return sharedSingleton;
}
}
-(id) init
{
if (self = [super init])
{
[self initDictionary];
}
return self;
}
I make the exact same call twice both in viewDidLoad
DAOController *daoController = [DAOController sharedSingleton];
self.teams = [daoController getTeamsForPlayer];
But in the 2nd it throws an exception or a EXC_BAD_ACCESS
2011-04-28 18:31:22.403 IScore[5637:207] -[NSKeyValueIvarSetter getTeamsForPlayer]: unrecognized selector sent to instance 0xa707220
2011-04-28 18:31:22.435 IScore[5637:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKeyValueIvarSetter getTeamsForPlayer]: unrecognized selector sent to instance 0xa707220'
Call stack at first throw:
The method simply does
-(NSMutableArray*) getTeamsForPlayer
{
NSMutableArray *teamsForPlayer = [[[NSMutableArray alloc] init] autorelease];
Team *team1 = [self.teams objectForKey:[NSNumber numberWithInt:1]];
[teamsForPlayer addObject:team1];
[team1 release];
return teamsForPlayer;
}
If I change the 2nd instance to non shared I can run the method without issue
DAOController *daoController = [[DAOController alloc]init];
Any assistance would be appreciated. Singleton pattern was taken from last entry on What should my Objective-C singleton look like?
Looks like your singleton has been deallocated and that another instance took its address.
You should check your code to find how this is possible. (you should never retain / release that singleton)
That's why I strongly suggest using Matt Gallagher's cocoawithlove singleton macro that you can download there, which is super easy and concise to use :
SYNTHESIZE_SINGLETON_FOR_CLASS(MyClassName);
It is a perfect singleton implementation, that avoid such issues by deallocating accidently your singleton, which looks like to be your problem. It is based on Apple recommandations, which overrides release, retainCount and such to protect your singleton.
method
-(id) initWithParent:(id)parent
{
// do something
}
calling of the above method
theM3u8Parser = [[M3u8Parser alloc] initWithParent:self];
That method works perfect. But now I also need to pass a NSString into the method.
So i changed it to
-(id) initWithParent:(id)parent:(NSString*)str
{
//do something
}
Then i call it like so
theM3u8Parser = [[M3u8Parser alloc] initWithParent:self:aStr];
But now the app crashes with a
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[M3u8Parser initWithParent:]: unrecognized selector sent to instance 0x6a10a50'
Is it not possible to modify this method? If so is there a way of accessing the string which is a member variable of the parent class?
Thanks
-Code
Sure it's possible to modify the method. Try it like this:
- (id)initWithParent:(id)parent andWithString:(NSString *)str;
Then call it like this:
theM3u8Parser = [[M3u8Parser alloc] initWithParent:self andWithString:aStr];
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.
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.
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.