假設我有兩個 inits 和一個私有財產,如下所示。
struct MyStruct {
let clousure: (Int, String, Bool) -> String
public init(clousure: @escaping (Int, String) -> String) {
/// How to assign this to the private property of MyStruct
self.clousure = ... ? /// THIS IS MY QUESTION
}
public init(clousure: @escaping (Int, String, Bool) -> String, ... ) {...}
有沒有辦法我如何包裝接受的clousure并將bool作為一些默認值提供,即false?提前致謝
uj5u.com熱心網友回復:
如果您想通過傳遞 clousure 和/或忽略部分論點來分享有關目標以及您試圖實作的目標的更多資訊,我們可以提供進一步的幫助。從代碼的角度來看,您傳遞的 @escaping 閉包似乎可以忽略布林值,如下所示:
struct MyStruct {
let clousure: (Int, String, Bool) -> String
public init(clousure: @escaping (Int, String) -> String) {
/// How to assign this to the private property of MyStruct
self.clousure = { i, str, _ -> String in
return clousure(i, str)
}
}
}
并按如下方式使用:
let s = MyStruct() { i, s -> String in return "string = \(s), int = \(i)"}
print(s.clousure(1, "hello", false))
導致輸出
string = hello, int = 1
我不確定您為什么要這樣做,也許您確實想要實作類似@Quack E. Duck 建議的目標,并在給定布林值的情況下獲得不同的結果:
struct MyStruct {
let clousure: (Int, String, Bool) -> String
public init(clousure: @escaping (Int, String) -> String) {
/// How to assign this to the private property of MyStruct
self.clousure = { i, str, increaseCount -> String in
if increaseCount { return clousure(i 1, str) }
else { return clousure(i, str) }
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496748.html
上一篇:收藏視圖不更新
