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

Python Kivy ListView: How to delete selected ListItemButton? -

asp.net mvc 4 - A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name '' -