PlayingCard inherits from Card
Given the two functions with the same name:
func match(othercards : [PlayingCard]) -> Int {
return 2
}
func match(othercards : [Card]) -> Int {
return 2
}
It is throwing an Error Saying : Overriding method with selector 'match:' has incompatible type '([PlayingCard]) -> Int'
Why??? Its two functions with same name but two different kind of parameters why is it still asking for override?? If I do that then even that is called as Error
Can you do this?
class Card {
func match(othercards : [Card]) -> Int {
return 2 // I changed the return value just to test it
}
}
class PlayingCard : Card {
func match(othercards : [PlayingCard]) -> Int {
return 1
}
}
Yes!
Specifically you can if Card does not extends NSObject.
class Card {
func match(othercards : [Card]) -> Int {
return 2 // I changed the return value just to test it
}
}
On the other hand, if Card
extends NSObject
.
class Card : NSObject {
func match(othercards : [Card]) -> Int {
return 2 // I changed the return value just to test it
}
}
Then you get the error!
It looks like overloading
only works on "pure" Swift classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With