I know I can call print directly, but why pass items to another function make it like wrapped by Array? I wanna know why and how to fix it
func test(items:Any...) {
print(items)
}
test(1,2,3) // print [1,2,3]
print(1, 2,3) // print 1 2 3
How to make test function act like print function?
Finally, I wrap test like this:
func test( items:Any...) {
for num in items {
print("\(num) ", separator:" ", terminator:"")
}
print("")
}
And this works fine, But Any better solutions?
If you want to make test
do the same thing as print
, why not just call print
?
func test(args: Any...) {
print(args)
}
If you are not allowed/don't want to use this little trick, you can try this. But this only works with CustomStringConvertible
:
func test(args: CustomStringConvertible...) {
print(args.map {
$0.description
}.joinWithSeparator(" "))
}
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