In Swift, how can you print out a variable multiple times. Say I had
var symbol = "*"
Can I do something like in JavaScript where you go
console.log(symbol * 4)
When I try to do this in Swift, an error comes back. Any way around this?
You can very easily create an operator overload which will make this work.
func * (left: String, right: Int) -> String {
var multipliedString = left
for x in 1..<right {
multipliedString += left
}
return multipliedString
}
Put that above your class, and then you can do something like:
println("Hello World" * 1000)
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