Question or problem in the Swift programming language:
Can someone please help me with this.
I have the following public enum
public enum OfferViewRow { case Candidates case Expiration case Description case Timing case Money case Payment }
And the following mutableProperty:
private let rows = MutableProperty<[OfferViewRow]>([OfferViewRow]())
In my init file I use some reactiveCocoa to set my MutableProperty:
rows <~ application.producer .map { response in if response?.application.status == .Applied { return [.Candidates, .Description, .Timing, .Money, .Payment] } else { return [.Candidates, .Expiration, .Description, .Timing, .Money, .Payment] } }
But now the problem, when I try to get the value of my enum inside my rows it throws errors. Please look at the code below.
func cellViewModelForRowAtIndexPath(indexPath: NSIndexPath) -> ViewModel { guard let row = rows.value[indexPath.row], let response = self.application.value else { fatalError("") } switch row { case .Candidates: // Do something case .Expiration: // Do something case .Description: // Do something case .Timing: // Do something case .Money: // Do something case .Payment: // Do something } }
It throws an error: Enum case 'some' not found in type 'OfferViewRow on the line let row = rows.value[indexPath.row]
And on every switch statements it throws: Enum case 'Candidates' not found in type '<>
Can someone help me with this?
How to solve the problem:
The guard statement wants an optional, as hinted by "Enum case 'some'" in the error message.
But rows.value[indexPath.row]
is not Optional<OfferViewRow>
, it is a raw OfferViewRow
. So it won't enter a guard statement.
Move let row = rows.value[indexPath.row]
one line up: Swift takes care of bounds checking, and will crash if indexPath.row is out of bounds.