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 Objective-C Managing Playlist Data Creating UIImageViews

Cant solve this problem

NSString *imageName = [friendsDict objectforKey:@"profilePicture"];
_profileImageName = [UIImage imageNamed:imageName];

I'm putting this block of code in user.m and its not working. please help me out

1 Answer

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Let's go through the assignments:

Let’s start by retrieving the image name as a string from the <code>friendsDict</code> dictionary and storing that in the <code>profileImageName</code> property of the User model. In User.m, use the key <code>profilePicture</code> to get the relevant information.

That is, the image name is stored separately as an NSString, so before creating an image from the name, assign the name to the profileImageName property:

_profileImageName = [friendsDict objectForKey:@"profilePicture"];

Please note, that in the code you provided you have a typo: objectforKey has to be objectForKey. Check the console output for errors, this code won't compile and gives you an error message.

Once you stored the image name, you are asked to also assign an image with this name to profileImage:

Now that we have the image name as a string, let’s create a UIImage instance using the <code>profileImageName</code> property to retrieve an image. Store this UIImage instance in the <code>profileImage</code> property of the User model. Hint: Use the <code>imageNamed:</code> UIImage class method.

_profileImage = [UIImage imageNamed:_profileImageName];

Your class should look like this at this point:

- (instancetype)init
    self = [super init];
    if (self) {
        Treehouse *treehouse = [[Treehouse alloc] init];
        NSDictionary *friendsDict = treehouse.friends;

        _firstName = [friendsDict objectForKey:@"firstName"];
        _lastName = [friendsDict objectForKey:@"lastName"];

        _profileImageName = [friendsDict objectForKey:@"profilePicture"];
        _profileImage = [UIImage imageNamed:_profileImageName];
    return self;

Hope that helps! :)