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: Sat, 30 Jul 2005 14:01:39 -0500
Thanks for you help guys, I;ve gotten a bit closer to solving my
problem. It seems I can't simply call an instance of a custom class by
making a variable with it as I had before:
ACRQuizProblemRowElement *newElement;
But instead needed to explicity allocate and initialize it:
ACRQuizProblemRowElement *newElement = [[ACRQuizProblemRowElement alloc] init];
Also taking your advice, my setters now utilize this format:
- (void) setContent: (NSString *)newContent
{
[_content release];
_content = [newContent copy];
}
This works fine in most situations, except when I am trying to set the array.
- (void) setAnswers: (NSArray *)newAnswers
{
[_answers release];
_answers = [NSArray copy];
}
This was called with:
[newElement setAnswers: [NSArray array]];
I am trying to replace the answers array with an empty array, but
apparently arrays don't work this way. I've even tried:
_answers = [NSArray arrayWithArray: newAnswers];
But either way I get a "EXC_BAD_ACCESS" from Debugger when I reach the
end of the addHeaderRow: method.
On 7/29/05, Steve Weller <email@hidden> wrote:
>
> Adam,
>
> Try my examples here: http://homepage.mac.com/bagelturf. I'm learning too and making all sorts of errors.
>
>
> On Friday, July 29, 2005, at 02:10PM, Adam Raney <email@hidden> wrote:
>
> >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
> >
> >
>
_______________________________________________
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