Skip to content

Instantly share code, notes, and snippets.

@armadsen
Created February 23, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armadsen/26f1feca57eaae00821c to your computer and use it in GitHub Desktop.
Save armadsen/26f1feca57eaae00821c to your computer and use it in GitHub Desktop.
Person.m for DevMountain Lesson 13
// For .h:
@interface Person : NSObject
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic, strong) NSString *job;
- (id)initWithDictionary:(NSDictionary *)dictionary;
- (NSDictionary *)personDictionary;
@end
// For Person.m:
static NSString * const imageNameKey = @"imageName";
static NSString * const nameKey = @"name";
static NSString * const phoneNumberKey = @"phoneNumber";
static NSString * const jobKey = @"job";
@implementation Person
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [super init];
if (self) {
self.imageName = dictionary[imageNameKey];
self.name = dictionary[nameKey];
self.phoneNumber = dictionary[phoneNumberKey];
self.job = dictionary[jobKey];
}
return self;
}
- (NSDictionary *)personDictionary {
NSMutableDictionary *entryDictionary = [NSMutableDictionary new];
if (self.name) {
[entryDictionary setObject:self.name forKey:nameKey];
}
if (self.imageName) {
[entryDictionary setObject:self.imageName forKey:imageNameKey];
}
if (self.phoneNumber) {
[entryDictionary setObject:self.phoneNumber forKey:phoneNumberKey];
}
if (self.job) {
[entryDictionary setObject:self.job forKey:jobKey];
}
return entryDictionary;
}
- (NSString *)description {
return self.name;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment