Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Playlist Browser with Objective-C Managing Playlist Data Using Helper Methods

Why do I need arrayWithArray method in this case

The instructor wrote:

_playlistArtists = [NSArray arrayWithArray: [playDictionary objectForKey:kArtist]];

The [playDIctionary objectForKey:kArtist] already returns an array. So, why do I still need arrayWithArray method ? why can't I do something like this:

_playlistArtists =[playDictionary objectForKey:kArtist];

Darean Wong
Darean Wong
Courses Plus Student 4,186 Points

I have this question too. Can someone please let us know why?

1 Answer

Your question is a very good example in understanding 'pointers'. If we set it equal like how you did, we would set the pointer '_playlistArtists' to the same address contained in 'playlistDictionary' (which is contained in property 'library' of the object 'musicLibrary') . Because we declared the property '_playlistArtists' as a strong reference, it will still work because the pointers created in the method won't be there after execution.

But I guess, it is just more intuitive to allocate a separate, new array which is a copy of the last one as the permanent one we will keep to store our data.