Mavs Mavs Mavs Mavs - 1 year ago 94
Swift Question

for loop of swift 3.0 index 1 beyond bounds [0 .. 0] error

before swift2.0 this is i use for forloop statement and im not getting error but when upgrading to swift3.0 the app always crash see below the one i use for forloop statement:

for (var i=0; i < results.count; i++)
let single_result = results[i]
let id = single_result.valueForKey("id") as! String
let name = single_result.valueForKey("name") as! String
let address = single_result.valueForKey("address") as! String
let mobile = single_result.valueForKey("mobile_number") as! String
let email = single_result.valueForKey("email") as! String
let gender = single_result.valueForKey("gender") as! String
let birth = single_result.valueForKey("birthday") as! String
user_id = id
gotID = id
gotName = name
gotAddress = address
gotEmail = email
gotGender = gender
gotMobile = mobile
gotBirth = birth

Im having error when i use this loop statement(swift3.0):

for i in 0.stride(through: results.count, by: 1)
for i in 0..<results.count

Answer Source

You're getting the index-off-by-one error.


 for i in 0.stride(through: results.count, by: 1)


 for i in 0.stride(through: results.count - 1, by: 1)

Note the - 1.