Re: newbie EXC_BAD_ACCESS
Re: newbie EXC_BAD_ACCESS
- Subject: Re: newbie EXC_BAD_ACCESS
- From: Charilaos Skiadas <email@hidden>
- Date: Mon, 21 Mar 2005 20:44:33 -0600
On Mar 21, 2005, at 8:03 PM, Daniel Child wrote:
Hi,
I'm not disagreeing with his approach. His four lines of code were
equivalent to my 20 or 25—and had the distinct advantage of actually
working!—but until I am so used to the idiom that I can write
extremely dense code like his, it might be good to break things up a
bit.
In Kochan and Hillegas I don't remember seeing any attempts to write +
(class) methods. I'm hoping to see how the same thing could be done
either way, and then understand what's the difference.
The class method is just a convenience method. What I would do actually
is implement both a class method and an instance method, so an instance
method would be:
- (id)initStrokeDescriptionWithType:...
where the actual code for handling things is.
Then I would have a class method:
+ (id)strokeDescriptionWithType:...
{
return [[[StrokeDescription alloc]
initStrokeDescriptionWithType:theType] autorelease];
}
I hope I got this right.
The idea is the same as with say NSStrings. Some times you don't really
want to keep a string around, but you need to use it in your loop. So
in your loop you could either have:
coolString=[[NSString alloc] initWithCoolProperties];
// do stuff with coolString
[coolString release];
Or you could have
coolString=[NSString stringWithCoolProperties];
// do stuff with coolString
// coolString is autoreleased, so we don't need to release it.
That's called a convenience method. It returns an autoreleased object,
so you don't have to release it. When you need objects temporarily, it
is *convenient* to not have to worry about releasing them. When you do
actually need to keep the items, then you need alloc/init.
ALL methods other than init/copy(/retain) return autoreleased objects.
The only advantage to using explicit alloc/init and release, versus the
convenience methods, is that id you happen to create thousands of
autoreleased objects in the same loop, this might cause trouble, since
they won't get released until later.
If you haven't yet, you would want to read this:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
It makes things a lot clearer (thanks mmalc).
Haris
Daniel
On Monday, March 21, 2005, at 03:02 PM, Charilaos Skiadas wrote:
I do think though, Daniel, that you would be better off, for
modularity, going with Hamish's suggestion of having your
StrokeDescription class implement a constructor that would return an
autoreleased object with the type and description provided, as in the
strokeDescriptionWithType, and structuring your code as he suggested.
_______________________________________________
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