[英]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.