ios - Display array from a value in the array -
so if have array this:
var myarray = ["bmw", "toyota", "ford", "lamborghini", "ferrari", "lada"]
i want display value inside array after "ford", bmw, toyota , ford doesn't show up.. how can this?
using swift slice, e.g:
myarray[3...myarray.count - 1]
output
["lamborghini", "ferrari", "lada"]
option 1
if don't know index of ford
element, find using indexof
method:
if let index = myarray.indexof("ford") { let startindex = index + 1 let endindex = myarray.count - 1 let slice = myarray[startindex...endindex] let array = array(slice) print(array) // prints ["lamborghini", "ferrari", "lada"] }
option 2
as @leo dabus pointed out, simpler method of getting section want array uses suffixfrom
method:
if let index = myarray.indexof("ford") { let slice = myarray.suffixfrom(index.successor()) let array = array(slice) print(array) // prints ["lamborghini", "ferrari", "lada"] }
Comments
Post a Comment