_cmd in Swift language [duplicate]

亡梦爱人 提交于 2019-12-12 07:47:41

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/24483379/cmd-in-swift-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!