Search code examples
swiftswift4

Binary operator '..<' cannot be applied to operands of type 'Int' and 'Int?'


I am getting a error after converting Objective-C to Swift. Thanks

var gymdays = GymHours.orderedDays()
for i in 0..<gymdays?.count {

}

error screen

My Objective-C code:

NSArray *days = [GymHours orderedDays];
for (uint i = 0; i < days.count; i++) {

}

Solution

  • Int? is optional, so Xcode is telling you you are trying to use a value that may or may not be there.

    You can use the following to avoid a crash if the value is nil:

    for i in 0..<(gymdays?.count ?? 0) {
    
    }