1

I am revisiting Apple Curriculum books and trying to complete exercises in different ways. The problem is simple: I am given an array, which I have to loop through to count votes.

    enum ClassTripDestination {
        case beach, chocolateFactory
    }
    
    let tripDestinationVotes: [ClassTripDestination] = [.beach, .chocolateFactory, .beach, .beach, .chocolateFactory]

The actual array has 200 values, I shortened it here so that it wouldn't take up too much space.


Solution is quite simple:

var beach = Int()
var factory = Int()

for destination in tripDestinationVotes {
    if destination == .beach {
        beach += 1
    } else {factory += 1}
    
}

But I decide to practice ternary operator and came up with this:

for destination in tripDestinationVotes {
    destination == .beach ? beach += 1 : factory += 1

}

And well, as my topic states, Xcode isn't happy with this code.

Left side of mutating operator isn't mutable: result of conditional operator '? :' is never mutable

But what bothers me is that right before this exercise I completed another - quite similar. I had to search through an array of chicken.

var chickenOfInterestCount = 0
for chicken in chickens {
    chicken.temper == .hilarious ? chickenOfInterestCount += 1 : nil
}
chickenOfInterestCount

And this code was executed without a single problem.

Can somebody explain to me, why is there a problem with counting the votes in the array?

MrMurman
  • 17
  • 8
  • Just try to add circle brackets for both case true and false. Like (beach += 1) – Raja Kishan Jun 04 '21 at 17:57
  • try `beach += destination == .beach ? 1 : 1` – Tushar Sharma Jun 04 '21 at 18:01
  • @Raja Kishan sometimes I do not understand what will Xcode do next. This really solved the issue. – MrMurman Jun 04 '21 at 18:05
  • @Tushar Sharma in order for that to work, the should be "0" after the colon. And it would only increase beach variable. – MrMurman Jun 04 '21 at 18:07
  • 1
    While others have answered your specific syntax issue, I would suggest instead that you consider using a `Dictionary` to model the vote counts. It's shorter and simpler, see: https://stackoverflow.com/a/41841238/3141234 – Alexander Jun 04 '21 at 18:55
  • @ Alexander that is definitely a more concise way to count array elements. But it touches upon more advanced topics like closures, which I only started looking into. – MrMurman Jun 04 '21 at 19:21

2 Answers2

1

The solution is simple just add rounded braces around both true and false part code.

for destination in tripDestinationVotes {
    (destination == .beach) ? (beach += 1) : (factory += 1)
}

I think with your code, Xcode just confused for having multiple signs like equal to or plus. But for second last case it’s clear that the false part is nil and identity the true part correctly

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • I'd put parentheses around `destination == .beach`, too. They're not required because `==` has higher precedence than `? :`, but they'll make it a lot easier to read. – Caleb Jun 04 '21 at 18:37
0

Well, it turns out that the solution was extremely easy. As Raja Kishan stated, I only had to put braces around beach += 1 like this:

for destination in tripDestinationVotes {
    destination == .beach ? (beach += 1) : (factory += 1)

}
MrMurman
  • 17
  • 8