• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: cannot be sent to an abstract object of class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cannot be sent to an abstract object of class


  • Subject: Re: cannot be sent to an abstract object of class
  • From: Sherm Pendley <email@hidden>
  • Date: Sat, 4 Dec 2010 19:06:22 -0500

On Sat, Dec 4, 2010 at 6:56 PM, Stephen J. Butler
<email@hidden> wrote:
> On Sat, Dec 4, 2010 at 5:17 PM, Shane
> <email@hidden> 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 (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

References: 
 >cannot be sent to an abstract object of class (From: Shane <email@hidden>)
 >Re: cannot be sent to an abstract object of class (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Re: cannot be sent to an abstract object of class
  • Next by Date: Re: [CFBundleIdentifier] Is is really a hyphen or a minus?
  • Previous by thread: Re: cannot be sent to an abstract object of class
  • Next by thread: NSView/NSTableView question
  • Index(es):
    • Date
    • Thread