senty senty - 1 year ago 131
Swift Question

Finding the index of 'NSURL item' in 'NSURL array'

I have 2 arrays of

and they have the same elements with different order. I am trying to use the array1's indexpath to get the member, and detect member's indexPath in the other array.

I couldn't figure out if I can detect the member's index on type NSURL. I thought one way could be using for loop for the second array, however the way I could think of was..

For loop through the array2 and convert each to string to have a [string] and using another for loop to find the index of the string that I'm looking for

let array1 = [NSURL]()
let array2 = [NSURL]()

array1 = [abc, qwe, jkl]
array2 = [jkl, abc, qwe]

// To wrap up..
// For jkl, I want to use array1[2] and get array2[?]

let searchIndex = 2

if array1[2].absoluteString.isNotEmpty {
let stringToSearch = array1[2].absoluteString


Answer Source
let index = array2.indexOf { $0.absoluteString == array1[2].absoluteString }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download