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

Fwd: method confusion


  • Subject: Fwd: method confusion
  • From: Hans van der Meer <email@hidden>
  • Date: Tue, 18 Sep 2007 10:22:32 +0200


On Sep 17, 2007, Charles Steinman wrote:


--- Hans van der Meer <email@hidden> 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;
[SNIP]
I fail to see why this is possible, as these
declarations are in
different classes.

But the compiler can't tell which class the method is being sent to. The alloc method has a return type of id, which could be anything. So when you send initWithSize: to the result of alloc, the compiler doesn't know what class it's talking to. I would

Strange I thought at first, that redefining the init methods to return (HMCharacterAlphabet *) instead of id did not make the error go away. But if the return of the alloc as id is the culprit, then this makes sense of course and it confirms the above diagnosis.

change the method name to something that doesn't
conflict, but if you have to keep it the same, you can
statically type it as [(HMCharacterAlphabet
*)[HMCharacterAlphabet alloc] initWithSize: 3]. This
will give the compiler the information it needs to use
the right method signature.

Indeed, this makes the error go away. But it is a solution I somehow
find unattractive because in all object creations this cast will be needed.
Renaming the method will be the most convenient I guess.
Then cross my fingers Apple will not introduce that one too later on.
Maybe I should resort to the solution of that problem given below.


On Sep 17, 2007, Ricky Sharp wrote:

(2) Suffix all ivars with _II (which then gave me unique accessors).
For example:
BOOL checked_II;
-(void)setChecked_II:(BOOL)flag;

Thanks to all people who did react to this question.

Hans van der Meer

_______________________________________________

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: Fwd: method confusion
      • From: Charles Steinman <email@hidden>
  • Prev by Date: how to get objects of type Class to play with NSCoder?
  • Next by Date: Re: how to get objects of type Class to play with NSCoder?
  • Previous by thread: Re: method confusion
  • Next by thread: Re: Fwd: method confusion
  • Index(es):
    • Date
    • Thread