[英]parameters without type annotation swift
我有一个函数来检查它所接受的参数类型,但我似乎很难定义一个可以使用任何类型注释的参数。 这是我一直在尝试的:
//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))
//
你回答了自己
[...]可以使用任何类型[...]
这是代码
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.