简体   繁体   中英

Swift: How to specify class candidate to use for a static method call

Let's say we have two Obj-C classes like these:

Parent {
   + (void)doSomething {
      NSLog(@"%@", [self messageToDisplay]);
   }

   + (NSString *)messageToDisplay {
      return @"message_parent";
   }
}

Child: Parent {
   + (NSString *)messageToDisplay {
      return @"message_child";
   }
}

And this Swift code:

Child.doSomething()

It generates this error because Swift does not know which candidate to use, Child or Parent.

Ambiguous use of 'doSomething()' Found this candidate (Child) Found this candidate (Parent)

How can we specify the candidate to use?

Many thanks.

Ok, got it. I found the cause of this issue. We have one ObjC method called deleteObjectWithId in the parent class and another one (useless by the way) called deleteGroupWithId it the child class and when calling the delete(withId:) method from Swift there are in fact two candidates. Many thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM