I have a swift array "Monthdata" that I want to append every second value in my months array.
var monthData = []
let months = ["Jul 12","Aug 12","Sep 12","Oct 12"]
for month in months {
self.monthData.append(month)
}
So basically I the monthData array to look like:
["Aug 12","Oct 12"]
Try to use modulo operator ( % )
var monthData = Array<String>()
let months = ["Jul 12","Aug 12","Sep 12","Oct 12"]
var i : Int = 1
for month in months{
if(i%2 == 0){
monthData.append(month)
}
i = i + 1
}
println(monthData)
Output :
[Aug 12, Oct 12]
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