Re: Crash when calling custom methods in custom subclass.
Re: Crash when calling custom methods in custom subclass.
- Subject: Re: Crash when calling custom methods in custom subclass.
- From: Adam Raney <email@hidden>
- Date: Fri, 29 Jul 2005 16:10:38 -0500
Unfortunately, I have tried learning from books, but find that I
learn much better by analyzing examples. While I will look for the
book you've suggested, if anyone could point out what I've done
wrong, and perhaps provide me with an example of a proper setter, I'd
greatly appreciate it.
On Jul 28, 2005, at 10:14 PM, Charilaos Skiadas wrote:
On Jul 28, 2005, at 9:34 PM, Adam Raney wrote:
ACRQuizProblemRowElement.h :
#import <Cocoa/Cocoa.h>
@interface ACRQuizProblemRowElement : NSObject
{
NSString * format;
NSString * content;
NSArray * answers; // to be filled with NSStrings
bool isHeader;
}
- (void) setFormat: (NSString *)newFormat;
- (void) setContent: (NSString *)newContent;
- (void) setAnswers: (NSArray *)newAnswers;
- (void) setIsHeader: (bool)newIsHeader;
@end
---------
ACRQuizProblemRowElement.m
...
- (void) setFormat: (NSString *)newFormat
{
format = [NSString stringWithString: newFormat];
}
All your accessors are wrong. Read up on setters and getters, and
the correct way to write them, and also on memory management. In
this particular case, stringWithString returns an autoreleased
object, which will be deallocated in all likelihood soon after the
method exits, and then "format" will point to garbage. Same goes
for all the other methods.
I would suggest, if you haven't done so yet, going through a book
on Cocoa programming, and getting the basics of Obj-C down right.
My personal favorite, and a lot of people will agree on that, is
Aaron Hillegass's book. It will make things immensely easier and
more clear.
- (void) setContent: (NSString *)newContent
{
content = [NSString stringWithString: newContent];
}
- (void) setAnswers: (NSArray *)newAnswers
{
answers = [NSArray arrayWithArray: newAnswers];
}
- (void) setIsHeader: (bool)newIsHeader
{
isHeader = newIsHeader;
}
Haris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden