gpt4 book ai didi

string - 快速关闭字符串

转载 作者:搜寻专家 更新时间:2023-10-31 22:22:16 25 4
gpt4 key购买 nike

在 Swift 中是否可以在运行时将闭包解析为字符串?例如:

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

应该给我 "x * 2 + 5"(例如函数调用 closureToString(myClosure))。有可能做这样的事情吗?顺便说一句,我的意思是在运行时,因为例如可以从命令行读取 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

关于string - 快速关闭字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com