我有一个函数来检查它接受的参数类型,但是我似乎很难定义一个可以与任何类型注释一起工作的参数。我一直在努力:
//bool
var x = 0, y = 3.142, z = "hello"
func checkType(input: /*here's my problem*/) -> String {
let res = "That is of type \(input.dynamicType)"
return res
}
print(checkType(//argument))
//发布于 2016-08-03 11:52:22
你自己回答
..。可以适用于任何类型..。
这是密码
func checkType(input: Any) -> String {
let res = "That is of type \(input.dynamicType)"
return res
}测试
checkType("Hello") // "That is of type String"
checkType(true) // "That is of type Bool"
checkType(123) // "That is of type Int"https://stackoverflow.com/questions/38742752
复制相似问题