0

What is the syntax for method hiding in swift? I've tried a bunch of options in the playground, but keep getting errors. Haven't been able to find any documentation on it either.

In superclass:

func performFunction() {
     print("performing function...")
}

In subclass, tried a couple different options that do not seem to work

new func performFunction() {
   print("function...")
}

and

func new performFunction() {
   print("function...")
}
Mike Laren
  • 8,028
  • 17
  • 51
  • 70
tara
  • 19
  • 1
  • 7
  • If you would've just started typing the method name, Xcode would've autocompleted the right syntax for you. – nhgrif Mar 29 '15 at 00:36
  • it keeps trying to autocorrect me to override the function, not hide it : ( – tara Mar 29 '15 at 00:54
  • is this not possible in Swift? – tara Mar 29 '15 at 00:54
  • 1
    You can't decrease the visibility of a method, if that's what you're asking. – nhgrif Mar 29 '15 at 00:56
  • Method hiding isn't possible in Swift, only overriding. Moreover, class methods (as implemented using the `static` keyword) are all final, so you can't even override at that level. – Nate Cook Mar 29 '15 at 01:07
  • Method "hiding" doesn't make a lot of sense to me... – nhgrif Mar 29 '15 at 01:10
  • Thank you Nate Cook! Have to do an assignment later (implementing classes in Swift, one part of it using method hiding) and thought I was crazy. This would explain why there is zero documentation on it... – tara Mar 29 '15 at 01:44

2 Answers2

1

You're looking for the override keyword:

class SubClass: ParentClass {
    override func performFunction() {
        println("function...")
    }
}

See the Swift Programming Language: Inheritance for more info.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Dexter
  • 18,213
  • 4
  • 44
  • 54
  • thank you but i do not mean overriding. i am looking for method hiding. (they are different) http://stackoverflow.com/questions/3838553/overriding-vs-method-hiding – tara Mar 29 '15 at 00:52
0

Question answered thanks to Nate Cook!

"Method hiding isn't possible in Swift, only overriding. Moreover, class methods (as implemented using the static keyword) are all final, so you can't even override at that level."

tara
  • 19
  • 1
  • 7