簡體   English   中英

Swift Closure to String

[英]Swift Closure to String

是否可以在Swift中在運行時解析閉包作為字符串? 例如:

let y = 5
let myClosure = { (x: Double) -> Double in
    return x * 2 + y
}

應該給我"x * 2 + 5" (例如函數調用closureToString(myClosure) )。 可以這樣做嗎? 順便說一句,我的意思是在運行時,因為y可以從命令行中讀取。

我不認為這是可能的,只是尋找確認^^謝謝;)

為什么在功能中需要它? 你不能做這樣的事嗎?

let y = 5
let myClosure = { (x: Double) -> Double in
    println("x * 2 + \(y), x = \(x)")
    return x * 2 + y
}
// Im assuming y is a parameter along with x if not remove it .
// Im returning tuples to access both stringValue and DoubleValue
    let myClosure = { (x: Double, y:Double) -> (DoubleValue: Double, StringValue:String) in
        return (x * 2 + y,"\(x) * 2 + \(y)")
    }


let MyClosureResult = myClosure(2,8)
// to accessString Value
MyClosureResult.StringValue
// to access DoubleValue 
MyClosureResult.DoubleValue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM