[英]How do I call a static method from another class in swift?
This is Selection class:这是选择 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: State class:
public func getActiveState() -> State? {
return Thread.current.threadDictionary[activeStateThreadDictionaryKey] as? State
}
How do I call getSelection()
in Text class?如何在文本 class 中调用
getSelection()
?
When I try to call the getSelection()
in Text class, let selection = getSelection()
I get this error - This method is defined on selection, and may not be available in this context.
当我尝试在文本 class 中调用
getSelection()
时, let selection = getSelection()
我收到此错误 - This method is defined on selection, and may not be available in this context.
I agree with @jnpdx we cannot see where getActiveState
is located.我同意@jnpdx 我们看不到
getActiveState
的位置。 Meaning...what class is it in for example.含义...例如,它是什么 class 。 You mention
getActiveState
is in a class but no additional information about that class.您提到
getActiveState
在 class 中,但没有关于该 class 的其他信息。 Aside from Access Controls on that class you should be able to declare and call in the following manner:除了 class 上的访问控制之外,您应该能够以下列方式声明和调用:
//Declaration: //宣言:
public class Selection {
public func getSelection() {
print("Get selection")
}
}
//Call Site: //调用站点:
let mySelection = Selection()
mySelection.getSelection()
Like @jnpdx mentioned we are not aware of the location of getActiveState
so we are not aware of the Access Controls around that class.就像@jnpdx 提到的,我们不知道
getActiveState
的位置,所以我们不知道 class 周围的访问控制。 That could be the culprit related to this particular issue.这可能是与此特定问题相关的罪魁祸首。 The access controls page in Swift Documentation is a handy read.
Swift 文档中的访问控制页面很容易阅读。 You can find that page at Swift Access Controls
您可以在Swift Access Controls找到该页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.