In Swift standard document by Apple :
func printInfo(_ value: Any) {
let type = type(of: value)
print("'\(value)' of type '\(type)'")
}
and it give an error : Variable used within its own initial value
How can I fix this with Swift 4.1?
That's a documentation error. The function used to be typeOf
. Recent version (can't remember which one) renamed it to type
. The compiler is getting confused between type
the local variable and type
the function in Swift's Standard Library.
Use a different name for your local variable:
func printInfo(_ value: Any) {
let t = type(of: value)
print("'\(value)' of type '\(t)'")
}
Or explicitly refer to the function:
func printInfo(_ value: Any) {
let type = Swift.type(of: value)
print("'\(value)' of type '\(type)'")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With