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

Re: method confusion


  • Subject: Re: method confusion
  • From: Ricky Sharp <email@hidden>
  • Date: Mon, 17 Sep 2007 16:50:08 -0500


On Sep 17, 2007, at 4:22 PM, Hans van der Meer wrote:

In my code I have in a testcase:
@interface HMCharacterAlphabetTest : SenTestCase {
...
Inside one of the tests:
HMCharacterAlphabet *alphabet = [[HMCharacterAlphabet alloc] initWithSize: 3];
defined in class HMCharacterAlphabet as:
- (id) initWithSize: (unsigned) size; // class in HMCharacterAlphabet.h


Somehow arises confusion with the identically typed init in NSImage.h:
- (id)initWithSize:(NSSize)aSize;

Compiling gives:
warning: multiple methods named -initWithSize
error: incompatible type for argument 1 of initWithSize
warning: using -(id)initWithSize:(NSSize)aSize (NSImage.h:65)

I fail to see why this is possible, as these declarations are in different classes.
Presumably it is some silly mistake. Can someone point me in the right direction?

I believe the issue here is that both initWithSize: methods are ultimately being sent to id.


I ran into a similar situation where one my custom control classes had this:

- (int)currentValue
{
    return [[self cell] currentValue];
}

When 10.4 was introduced, NSAnimation added a -(float)currentValue API. The scary thing was that the code compiled. At runtime, the control actually sent NSAnimation's message instead! This is because [self cell] returns an id. It then decided to send the animation's message (probably since it was picked up first due to importing <Cocoa/Cocoa.h> in my precomp header).

In your case, it looks like you do have -Wstrict-selector-match in your 'Other C Flags' options. That's a good thing (and other developers should consider turning that one on to catch these situations).

There are then two workarounds:

(A) explicit cast. Not always good. In my control's API, the workaround would look like this:

return [(IIValueFieldCell*) [self cell] currentValue];

But, I decided to do (B) instead:


(B) Due to lack of namespacing, I made three key changes so that no collisions would ever occur:


(1) All class names, constants, globals, structs, typedefs prefixed with II. Most folks are doing this; picking a unique 2-letter prefix.

(2) Suffix all ivars with _II (which then gave me unique accessors). For example:

BOOL checked_II;
-(BOOL)checked_II;
-(void)setChecked_II:(BOOL)flag;

I did the same thing for any custom bindings.

(3) Suffix all methods (before first arg) with _II:

- (int)computeSum_II
- (void)initWithStudent_II:(IIStudent*)aStudent
- (void)initWithStudentName_II:(NSString*)aName settings:(IISettings*) someSettings;


messages are thus computeSum_II, initWithStudent_II: and initWithStudentName_II:settings:


YMMV

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com

_______________________________________________

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: 
 >method confusion (From: Hans van der Meer <email@hidden>)

  • Prev by Date: Re: method confusion
  • Next by Date: Re: method confusion
  • Previous by thread: Re: method confusion
  • Next by thread: Fwd: method confusion
  • Index(es):
    • Date
    • Thread