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 trialJac Wynn
16,850 PointsCreating instance of UIImageView
How do I allocate and initialize the userProfilePhoto property to use the profileImage that was created?
This maybe be something simple but im confused!
#import <Foundation/Foundation.h>
@interface Treehouse : NSObject
@property (strong, nonatomic) NSDictionary *friends;
@end
#import "Treehouse.h"
@implementation Treehouse
- (instancetype)init
{
self = [super init];
if (self) {
_friends = @{@"firstName": @"Susan",
@"lastName": @"Olson",
@"profilePicture": @"susan_profile.png"
};
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *profileImageName;
@property (nonatomic, strong) UIImage * profileImage;
@property (nonatomic, strong) UIImageView *userProfilePhoto;
@end
#import "User.h"
#import "Treehouse.h"
@implementation User
- (instancetype)init
{
self = [super init];
if (self) {
Treehouse *treehouse = [[Treehouse alloc] init];
NSDictionary *friendsDict = treehouse.friends;
_firstName = [friendsDict objectForKey:@"firstName"];
_lastName = [friendsDict objectForKey:@"lastName"];
//Enter your code below!
_profileImageName = [friendsDict objectForKey:@"profilePicture"];
_profileImage = [UIImage imageNamed:_profileImageName];
}
return self;
}
- (UIImageView *)
@end
1 Answer
Chris Shaw
26,676 PointsHi Jac,
As per the task it asks you to create a new instance of UIImageView
using the initWithImage
convenience constructor which accepts an instance of UIImage
, so what we need to do is allocate a new instance of UIImageView and pass our
profileImage` property to it like below.
_userProfilePhoto = [[UIImageView alloc] initWithImage:_profileImage];
Hope that helps.
Jac Wynn
16,850 PointsJac Wynn
16,850 PointsThanks that worked out for me!
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsNo worries.