[英]How do I call a static method from another class in swift?
这是选择 class:
public class Selection {
var anchor: Point
var focus: Point
var dirty: Bool
var format: Int
init(anchor: Point, focus: Point, format: Int) {
self.anchor = anchor
self.focus = focus
self.dirty = false
self.format = format
}
public func getSelection() -> Selection? {
let state = getActiveState()
return state?.selection
}
}
State class:
public func getActiveState() -> State? {
return Thread.current.threadDictionary[activeStateThreadDictionaryKey] as? State
}
如何在文本 class 中调用getSelection()
?
当我尝试在文本 class 中调用getSelection()
时, let selection = getSelection()
我收到此错误 - This method is defined on selection, and may not be available in this context.
我同意@jnpdx 我们看不到getActiveState
的位置。 含义...例如,它是什么 class 。 您提到getActiveState
在 class 中,但没有关于该 class 的其他信息。 除了 class 上的访问控制之外,您应该能够以下列方式声明和调用:
//宣言:
public class Selection {
public func getSelection() {
print("Get selection")
}
}
//调用站点:
let mySelection = Selection()
mySelection.getSelection()
就像@jnpdx 提到的,我们不知道getActiveState
的位置,所以我们不知道 class 周围的访问控制。 这可能是与此特定问题相关的罪魁祸首。 Swift 文档中的访问控制页面很容易阅读。 您可以在Swift Access Controls找到该页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.