14

My previous question was marked duplicate. I tried to edit my question, but I can't remove duplicate tag, so I have to create a new one What is the swift equivalent to _cmd?

I want to get current method name to use in a format message similar to this one

[NSExeception raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]

Also, I want to use _cmd as key to set associated object. Does anyone know the equivalent of _cmd in Swift I really appreciate.

Community
  • 1
  • 1
LongNV
  • 892
  • 9
  • 21

1 Answers1

23

There's no _cmd, but you can use __FUNCTION__ to get the name of the current function, which can be used in place of selectors most of the time.

func myUnimplementedMethod() {
    println("You must override \(__FUNCTION__) in a subclass")
}
myUnimplementedMethod()
// prints "You must override myUnimplementedMethod() in a subclass"
Nate Cook
  • 92,417
  • 32
  • 217
  • 178