• 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: Convenience Methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convenience Methods


  • Subject: Re: Convenience Methods
  • From: Alastair Houghton <email@hidden>
  • Date: Wed, 26 Sep 2007 14:23:21 +0100

On 26 Sep 2007, at 13:39, Tim Davis wrote:

On Sep 26, 2007, at 7:03 AM, Alastair Houghton wrote:

Another downside of using id is that it means that the compiler sometimes has to guess which method you mean. For instance, if you make a method called -size that returns (e.g.) a size in bytes, and you call it via an id, you'll probably find that the compiler thinks it should return an NSSize and not whatever your actual method returns. That's because it doesn't know the type, so it looks through the messages it knows about and the first one it finds called "size" happens not to be the one you declared. (I've had this happen on a couple of occasions... I'm sure I even managed to cause a bug somewhere because of this "guessing" behaviour, though I don't remember the details).

The above paragraph is exactly why I posed this question in the first place. The class overrides a couple properties and the compiler generates warnings about which one is being called. So far nothing is working improperly, but I want to make sure that later down the line I'm not getting weird nondescript bugs that I can't find because of this.


And it's being init'd like this:

id newInstance = [[[self class] alloc] init];

 So should it be:

MyClass newInstance = [[MyClass alloc] init];

Well

  MyClass *newInstance = [[MyClass alloc] init];

is one way to do it, yes. If you do that, then the compiler knows the type of object whose methods it's supposed to be invoking, so you shouldn't get any warnings.

Sometimes when I'm writing convenience constructors I might write

  + (MyClass *)myClassWithFoo:(int)f
  {
     return [[[self alloc] initWithFoo:f] autorelease];
  }

where "self" is, of course, the class. A good reason to do that is that it means that a subclass only has to override -initWithFoo: in order to make +myClassWithFoo: work as expected.

If you find that you have problems, you can obviously assign to a variable with a more specific type, or you can just cast. For instance, if there happens to be some other -initWithFoo: method with a different parameter type that is causing problems, you could write

     return [[(MyClass *)[self alloc] initWithFoo:f] autorelease];

instead.

I should also mention that, thankfully, the problem of confusion of methods with the same name is fairly unusual, at least in my experience. And when it does happen, you can easily resolve it, either by renaming one of the methods, or by giving the compiler more type information.

Kind regards,

Alastair.

--
http://alastairs-place.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: 
 >RE: Convenience Methods (From: Jeff Laing <email@hidden>)
 >Re: Convenience Methods (From: Alastair Houghton <email@hidden>)
 >Re: Convenience Methods (From: Tim Davis <email@hidden>)

  • Prev by Date: Re: Console: *** -observeValueForKeyPath:... only defined for abstract class
  • Next by Date: General NSView Query
  • Previous by thread: Re: Convenience Methods
  • Next by thread: RE: Convenience Methods
  • Index(es):
    • Date
    • Thread