【发布时间】:2014-07-23 08:47:44
【问题描述】:
我似乎在文档中找不到它,我想知道它是否存在于原生 Swift 中。例如,我可以像这样在NSTimer 上调用类级函数:
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "someSelector:", userInfo: "someData", repeats: true)
但我似乎找不到使用自定义对象的方法,因此我可以这样称呼它:
MyCustomObject.someClassLevelFunction("someArg")
现在,我知道我们可以将 Objective-C 与 Swift 混合使用,NSTimer 类方法可能是这种互操作性的残余。
问题
Swift 中是否存在类级函数?
如果是,如何在 Swift 中定义类级函数?
【问题讨论】:
-
class func functionName() -> returnType顺便说一句,您可以检查现有的框架类以找到答案,例如NSNotificationCenter类的defaultCenter()方法。 -
@NSUInteger - 我只是在寻找 Objective-C 头文件。你能给我指点 NSNotificationCenter.swift 吗?
-
好的。尝试这个。在代码中输入 NSNotificationCenter.defaultCenter() 然后使用 Command+click 导航到 swift 文件
-
@nsuinteger -- 谢谢,我是点击选项,它只带我到 ObjC 标题。
-
很高兴你把它整理出来。可选择安装 ios8 库和 xcode 库,这样您也将拥有文档(如果尚未完成)。 xcode > 首选项 > 下载 > ios8 库和 xcode6 库。
标签: swift