for loop - Iterating through array to find occurrences of value -



for loop - Iterating through array to find occurrences of value -

i have been trying iterate through array check how many times 2 particular values occur. came code:

var paymentnum = 0 var creditnum = 0 index in images { if images.objectatindex(index) == 0 { paymentnum = paymentnum + 1 } else { creditnum = creditnum + 1 } }

this doesn't seem work though. error 'anyobject' not convertible 'int'.

where going wrong?

i'm making pretty huge assumptions because don't have much detail what's going on. i'm assuming images nsarray. nsarray in objective-c translates [anyobject] in swift. when have for-each style loop this

for value in array {}

the loop iterate through each value in array. value actual object in array, not counter in for(int = 0; < 10; i++) style loop

so you're expecting is

for item in images { if item == 0 { paymentnum++ } else { creditnum++ } }

you might need cast loop [anyobject] [int] this

let tmp = images [int] item in tmp {...}

or, cast value pulled out of array this

for item in images { if (item int) == 0 { paymentnum++ } else { creditnum++ } }

but former preferable

arrays for-loop swift nsarray

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -