Swift 字符串闭包
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
在 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