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 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
PLUS
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:

user.m
- (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! :)