Swift Array - Check if an index exists -


in swift, there way check if index exists in array without fatal error being thrown?

i hoping this:

let arr: [string] = ["foo", "bar"] let str: string? = arr[1] if let str2 = arr[2] string? {     // wouldn't run     println(str2) } else {     // run } 

but get

fatal error: array index out of range

an elegant way in swift:

let isindexvalid = array.indices.contains(index) 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -