mercredi 6 avril 2016

more elegant code for for if in swift

I got a simple code which works and which I am programming in and old fashioned way and I am sure there is a more elegant way of doing this in swift. Here is the code:

    var cardsInCompartment1:Int = 0
    var cardsInCompartment2:Int = 0
    for card in cards{
        if card.compartment == 1{
            cardsInCompartment1 += 1
            print(cardsInCompartment1)
        }
        if card.compartment == 2{
            cardsInCompartment2 += 1
            print(cardsInCompartment2)
        }
    }

I basically got cards in different compartments and now I want to count how many cards are in each compartment.

Aucun commentaire:

Enregistrer un commentaire