Passing parameters with initWithParent methods - iphone

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];

Related

App crashes and Program receives error signal SIGABRT

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.

Exception occur in facebook sample

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Facebook
authorize:delegate:]: unrecognized selector sent to instance
0x684fe80'
- (void)loginToFacebook:(id) loginDelegate
{
NSLog(#"login facebook method");
fbServiceRequestingobj = loginDelegate;
NSArray* permissions = [[NSArray alloc] initWithObjects:#"publish_stream", nil];
[facebook authorize:permissions delegate:self];
}
the message is pretty straightforward: the parameter loginDelegate, which you set as the delegate, does not respond to the selector. to verify the parameter when you set it:
- (void)loginToFacebook:(id)loginDelegate
{
assert([loginDelegate respondsToSelector:#selector(authorize:delegate:)]);
chances are good in this scenario, that the selector in question is a #required method for the protocol you are expected to adopt. if so, then the parameter you pass as loginDelegate would need to implement the method authorize:delegate: declared in the protocol.
when adopting a protocol, the compiler can inform you if you do not implement the required methods.
[facebook authorize:permissions delegate:self];
Does this method exist for the "facebook" object? I'd imagine not, as it is crashing saying that this method doesn't exist, hence the "unrecognized selector sent to instance".

unrecognized selector sent to instance

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?

Issues with Singleton in objective C

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.

IBAction being called NSCFDictionary and crashing app

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.