On Sat, Dec 4, 2010 at 6:56 PM, Stephen J. Butler <stephen.but...@gmail.com> wrote: > On Sat, Dec 4, 2010 at 5:17 PM, Shane > <software.research.developm...@gmail.com> wrote: >> @interface >> { >> NSNumber *rate; >> // ... >> } >> // ... >> #end >> >> - (id) init >> { >> // … >> rate = [[NSNumber alloc] initWithFloat:0.0]; >> // now has retain count of 1 >> } >> >> - (void) myMethod >> { >> // ... >> [rate initWithFloat:[[config rate] floatValue]]; >> // … >> } >> >> *** initialization method -initWithFloat: cannot be sent to an >> abstract object of class NSCFNumber: Create a concrete instance! > > alloc and init* must come as a pair, and only once. You can't call any > init* method on an object a second/third/etc time. > > More to the point, NSNumber is an immutable class. If you want to > change the value, you have to alloc/init another one.
In addition, you should be using accessor methods; otherwise, you'll end up with -release calls scattered through your code, wherever you need to replace rate with a newly-created NSNumber instance. A properly-written accessor method encapsulates the -release of the old value and -retain of the new one into one place, which makes it easy to debug when something goes wrong. Property syntax takes this one step further, automating the process of creating accessor methods - but IMHO you should write them by hand a few times, just to familiarize yourself with what the various property attributes (copy, retain, assign) actually do for you. sherm-- -- Cocoa programming in Perl: http://camelbones.sourceforge.net _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com