Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

the find function doenst exist in my xcode and i have the latest update

here its my error: 'find' is unavailable: call the 'indexOf()' method on the collection

     let playlistImageView = sender!.view as! UIImageView
        if let index = find(playlistArray, playlistImageView) {

        }

anyone can help? when i write find it only appears in the completer window a FIND in Upercase but the find fnction of the video doenst appears

1 Answer

David,

Do you know what version of Swift you are using?

Available is indexOf(), which will return the index of a given element, or it will return nil if the element does not exist in a given array.

Here is an example:

    let arrayOfNames = ["Alex",
                      "Brittany",
                      "Christopher",
                      "Denise",
                      "Eric",
                      "Francesca"]


    if let indexOfEric = arrayOfNames.indexOf("Eric") {
        let eric = arrayOfNames[indexOfEric]
        print(eric)
    } else {
        print("Eric does not exist.")
    }

The code above will print Eric.

    if let indexOfGregory = arrayOfNames.indexOf("Gregory") {
        let gregory = arrayOfNames[indexOfGregory]
        print(gregory)
    } else {
        print("Gregory does not exist.")
    }

The code above will print nil.

Using indexOf(), if the element that you are looking for exists, you will be given the index. Then, you can just access that element by passing that index to the array that contains it.

Hope this helps!