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 Build a Playlist Browser with Swift Refactoring Our Code Passing Playlist Objects Using Segues

Waseef Akhtar
seal-mask
.a{fill-rule:evenodd;}techdegree
Waseef Akhtar
Full Stack JavaScript Techdegree Student 2,802 Points

find() is no more supported with Swift 2.0.

I've been practicing this course with Swift 2.0 and everything just worked perfectly up until now. I'm trying to create the find() function but it's not available in the Xcode Version 7.0 Beta 3.

Please help if there's a similar solution to this problem.

Sasha Kiselev
Sasha Kiselev
2,382 Points

I am also using Xcode 7 beta 3 same problem, please help anyone

8 Answers

Sasha Kiselev
Sasha Kiselev
2,382 Points

After googling for hours I have found how to retrive the index of an array element in Swift 2.0 from this stackoverflow http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-apples-swift here is how to use it if let index = playlistArray.indexOf(playlistImageView) then function is indexOf which is a method of all arrays searching for ImageView. I can confirm it works

David Maybach
David Maybach
2,092 Points
let playlistImageView = sender!.view as! UIImageView
            if let index = playlistArray.indexOf(playlistImageView) {
                let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
                playlistDetailController.playlist = Playlist(index: index)
            }

I couldn't get it to work. Not sure what you meant by "then func is indexOF

Thank You, it works!!:)

Sasha Kiselev
Sasha Kiselev
2,382 Points

the code i used is let playlistImageView = sender!.view as! UIImageView if let index = playlistArray.indexOf(playlistImageView) { let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController playlistDetailController.playlist = Playlist(index: index) } are you sure you are using the latest xcode 7 version?

Lydia Khashina
Lydia Khashina
3,015 Points

Sasha, I used your code but I've run into an error '[UIImageView]' does not have a member named 'indexOf' in the line:

if let index = playlistArray.indexOf(playlistImageView) {}

Could you please help me? :)

P.S. Are you from Russia? :)

michael de marigny
michael de marigny
3,411 Points

Thanks Sasha, works perfect with version 7 beta 4

Sasha Kiselev
Sasha Kiselev
2,382 Points

I am from Russia, the problem you might have is that you are using a different version of XCode, the version in which this code works is the XCode 7 betas

Lydia Khashina
Lydia Khashina
3,015 Points

I've already solved my problem, thanks.

Я тоже из России. Приятно видеть здесь своих :)

Saam Zonoozi
Saam Zonoozi
1,962 Points

Lydia, how did you solve your problem? I'm also getting [UIImageView] does not have a member named indexOf. I'm using XCode 6.4.

Sasha Kiselev
Sasha Kiselev
2,382 Points

This is a solution for the XCode 7 betas in XCdoe 6 you should just follow the video

Kris Larsen
Kris Larsen
8,015 Points

I have XCode 7 but Sasha Kislev's code does not work for me.

can someone tell me why?

Rick Matsumoto
Rick Matsumoto
2,547 Points

Kris, I'm running Xcode 7.1.1 and got the following code to work. Make sure you have the bangs(!). Also, if you just pasted Sasha's code snippet above, note that playlistArray should be playlistsArray.

let playlistImageView = sender!.view as! UIImageView
if let index = playlistsArray.indexOf(playlistImageView) {
     let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
     playlistDetailController.playlist = Playlist(index: index)
     }

I can confirm that Rick's answer works on the very recent version of Xcode (7.2)!

Van Doan
Van Doan
32,285 Points

The above issue could be a syntax one as well.

David's and Sasha's has it: if let index = playlistArray.indexOf(playlistImageView) {

Where as Rick has it: if let index = playlistsArray.indexOf(playlistImageView) {

Notice the missing "s". So if you get an error about an unresolved identifier, it's probably that.