Re:
Re:
- Subject: Re:
- From: Daniel Child <email@hidden>
- Date: Wed, 22 Aug 2007 12:01:30 -0400
Kirk,
Thanks for all the many code suggestions. I especially appreciate the
list of advice below, as memory issues have been taking up lots of my
time. The idea of using a factory method [NSMutableArray array] for
the array was especially helpful since I wanted a basic empty array
to which I could later add objects.
Your list of advice brings up host of questions. Specifically, (in
reference to your points)
#1 (always use accessors)
Would you recommend using accessors even for primitive instance
variables (int, BOOL, etc.), where there is no need to retain, copy,
etc.
#3 (when no factory, create as category on the class.)
I have been creating "constructors" (and I guess you don't call them
that in Obj-C) using something like:
+ myClass {
MyClass *mc = [[[MyClass alloc] init] autorelease];
return mc;
} // other constructors like +myClass:withValue: to follow
Will this pattern be safe? Or is there a better approach.
#5 - #6 (use immutable objects whenever possible) Why the emphasis on
immutable objects? Simply because they are more efficient?
#7 (use [self setThing: nil] in dealloc)
Kochan had [iVar release] in his dealloc methods. What is the
advantage of using [self setThing: nil] instead? (7)
#8 (use a class accessor for class globals etc.)
???? I have no idea how to do 8. I expect to need plenty of class-
related constants. For example, my singleton class (a string parser
to parse input into syllables) has a list of values against which
input text is to be checked. I stored those values in an NSSet which
is one of the instance variables. You are recommending something else?
Thanks....
Daniel
On Aug 22, 2007, at 6:29 AM, Kirk Kerekes wrote:
Advice on how to be able to concentrate on your app and not on
memory management:
1. Always use accessors. Read up on accessors and figure out what
accessor pattern suits your usage, and use a configurable accessor
generator.
2. Always use class factory methods (like [NSMutableArray array]
below) when available.
3. When no factory method exists, create one as a category on the
class, and see #2.
4. Outside of accessors and factory methods, treat any use of alloc/
init or retain/release as suspicious, and examine it carefully
to see if it can be reworked to conform to {1,2,3} above.
5. Use immutable objects when appropriate (IE, NSString instead of
NSMutableString).
6. Always use immutable objects as dictionary keys.
7. In your -dealloc method, use [self setThing: nil] pattern to
release and zero out all object IVs.
8. If you have a class-related constant or a class-related global,
consider using a class accessor instead of a #define or other C-
level technique:
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
- Follow-Ups:
- Re:
- From: "Shawn Erickson" <email@hidden>