首页 > 代码库 > 一道例题的详解
一道例题的详解
实现中等难度通讯录。需求:
1、定义联系人类AddressContact。实例变量:姓名(拼音,?字母大写)、性别、电话号码、住址、分组名称、年龄。?法:?定义初始化方法(姓名、电话号码)、显?联系人信息
2、在main.m中定义字典,分组管理所有联系人。分组名为26个?写的英文字母。
3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。
4、获取某个分组名称下所有联系人,并且按照姓名升序排列。
5、从通讯录中根据电话号码搜索联系人。
6、获取所有女性的联系人,并且按照年龄的降序排列。
7、根据姓名删除某个联系人。
8、删除某个分组的全部联系人。
Contact.h中代码
1 #import <Foundation/Foundation.h> 2 3 //这是一个联系人类,用于存储单个联系人的信息 4 //并且能够提供联系人相关内容 5 //这个类是一个Model类 6 7 @interface Contact : NSObject 8 { 9 NSString *_name; //姓名10 NSString *_sex; //性别11 NSString *_phone; //电话12 NSString *_address; //地址13 NSString *_groupName; //组名14 NSInteger _age; //年龄15 }16 //初始化方法,初始化的时候需要给定一个姓名和电话17 - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone;18 19 - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex age:(NSInteger)age;20 21 //显示联系人信息22 - (void)displayInfo;23 24 //获取联系人姓名25 - (NSString *)name;26 27 //获取联系人电话28 - (NSString *)phone;29 30 //获取联系人分组信息31 - (NSString *)groupName;32 33 //获取联系人性别34 - (NSString *)sex;35 36 - (NSInteger)age;37 38 //联系人按姓名比较39 - (NSComparisonResult)compareByName:(Contact *)contact;40 41 //联系人按年龄比较42 - (NSComparisonResult)compareByAge:(Contact *)contact;43 44 @end
Contact.m中代码
1 #import "Contact.h" 2 3 @implementation Contact 4 5 - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone 6 { 7 return [self initWithName:name phone:phone sex:nil age:0]; 8 } 9 //指定初始化方法10 - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex age:(NSInteger)age11 {12 self = [super init];13 if (self) {14 _name = name;15 _sex = sex;16 _groupName = [_name substringToIndex:1];17 _age = age;18 _phone = phone;19 }20 return self;21 }22 23 - (void)displayInfo24 {25 NSLog(@"name:%@ sex:%@ phone:%@ address:%@ group:%@ age:%ld",_name,_sex,_phone,_address,_groupName,_age);26 }27 28 29 - (NSString *)name30 {31 return _name;32 }33 34 - (NSString *)sex35 {36 return _sex;37 }38 39 - (NSString *)phone40 {41 return _phone;42 }43 44 45 - (NSString *)groupName46 {47 return _groupName;48 }49 50 - (NSComparisonResult)compareByName:(Contact *)contact51 {52 NSString *myName = [self name];53 NSString *otherName = [contact name];54 NSComparisonResult result = [myName compare:otherName];55 if (result == NSOrderedAscending) {56 return NSOrderedDescending;57 }else if (result == NSOrderedDescending){58 return NSOrderedAscending;59 }else {60 return NSOrderedSame;61 }62 // return result;63 }64 65 - (NSInteger)age66 {67 return _age;68 }69 70 - (NSComparisonResult)compareByAge:(Contact *)contact71 {72 NSInteger myAge = [self age];73 NSInteger otherAge = [contact age];74 if (myAge > otherAge) {75 return NSOrderedAscending;76 }else if (myAge < otherAge){77 return NSOrderedDescending;78 }else {79 return NSOrderedSame;80 }81 }82 83 @end
ContactManager.h中代码
1 #import <Foundation/Foundation.h> 2 #import "Contact.h" 3 4 //这个类是一个联系人管理类,主要用于添加,删除,获取,搜索联系人等 5 6 @interface ContactsManager : NSObject 7 { 8 //用于存储所有联系人的字典 9 NSMutableDictionary *_contactsDic;10 }11 - (instancetype)init;12 13 //添加联系人14 - (void)addContact:(Contact *)contact;15 16 //根据groupName获取一组联系人17 - (NSArray *)contactsForGroupName:(NSString *)groupName;18 19 //根据电话号码,获取联系人20 - (Contact *)contactForPhone:(NSString *)phone;21 22 //根据性别,获取联系人23 - (NSArray *)contactsForSex:(NSString *)sex;24 25 //删除指定姓名的联系人26 - (void)removeContactForName:(NSString *)name;27 28 //删除指定分组的所有联系人29 - (void)removeContactsForGroup:(NSString *)groupName;30 31 //显示所有人的信息32 - (void)displayAllContacts;33 34 @end
ContactsManager.m中代码
1 #import "ContactsManager.h" 2 3 @implementation ContactsManager 4 5 -(instancetype)init{ 6 self = [super init]; 7 if (self) { 8 _contactsDic = [[NSMutableDictionary alloc] initWithCapacity:26]; 9 } 10 return self; 11 } 12 13 - (void)addContact:(Contact *)contact 14 { 15 NSString *name = [contact name]; 16 NSString *phone = [contact phone]; 17 if ([name isEqualToString:@""] || name == nil || [phone isEqualToString:@""] || phone == nil) { 18 return; 19 } 20 21 NSString *groupName = [contact groupName]; 22 NSMutableArray *group = [_contactsDic objectForKey:groupName]; 23 if (group == nil) { 24 NSLog(@"分组不存在"); 25 //创建一个数组,把人放数组里,把数组放字典里 26 group = [[NSMutableArray alloc] initWithCapacity:10]; 27 [group addObject:contact]; 28 [_contactsDic setObject:group forKey:groupName]; 29 }else { 30 [group addObject:contact]; 31 } 32 } 33 34 - (NSArray *)contactsForGroupName:(NSString *)groupName 35 { 36 NSMutableArray *group = [_contactsDic objectForKey:groupName]; 37 [group sortUsingSelector:@selector(compareByName:)]; 38 return group; 39 } 40 41 - (NSArray *)contactsForSex:(NSString *)sex 42 { 43 NSMutableArray *results = [[NSMutableArray alloc] initWithCapacity:10]; 44 45 for (NSString *key in _contactsDic) { 46 NSMutableArray *group = [_contactsDic objectForKey:key]; 47 for (Contact *c in group) { 48 NSString *s = [c sex]; 49 if ([s isEqualToString:sex]) { 50 [results addObject:c]; 51 } 52 } 53 } 54 55 [results sortUsingSelector:@selector(compareByAge:)]; 56 57 return results; 58 } 59 60 - (Contact *)contactForPhone:(NSString *)phone 61 { 62 for (NSString *key in _contactsDic) { 63 NSMutableArray *group = [_contactsDic objectForKey:key]; 64 for (Contact *c in group) { 65 NSString *p = [c phone]; 66 if ([p isEqualToString:phone]) { 67 return c; 68 } 69 } 70 } 71 return nil; 72 } 73 74 - (void)removeContactForName:(NSString *)name 75 { 76 NSMutableArray *arr = nil; 77 Contact *toDelete = nil; 78 for (NSString *key in _contactsDic) { 79 NSMutableArray *group = [_contactsDic objectForKey:key]; 80 for (Contact *c in group) { 81 NSString *n = [c name]; 82 if ([n isEqualToString:name]) { 83 arr = group; 84 toDelete = c; 85 } 86 } 87 } 88 [arr removeObject:toDelete]; 89 } 90 - (void)removeContactsForGroup:(NSString *)groupName 91 { 92 [_contactsDic removeObjectForKey:groupName]; 93 } 94 95 - (void)displayAllContacts 96 { 97 NSArray *allKeys = [_contactsDic allKeys]; 98 NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)]; 99 for (NSString *key in sortedKeys) {100 NSMutableArray *group = [_contactsDic objectForKey:key];101 NSLog(@"---%@---",key);102 for (Contact *c in group) {103 [c displayInfo];104 }105 }106 107 108 // for (NSString *key in _contactsDic) {109 // NSMutableArray *group = [_contactsDic objectForKey:key];110 // NSLog(@"---%@---",key);111 // for (Contact *c in group) {112 // [c displayInfo];113 // }114 // }115 }116 117 @end
main.m中代码
1 #import <Foundation/Foundation.h> 2 #import "ContactsManager.h" 3 4 int main(int argc, const char * argv[]) 5 { 6 // NSString *allColorString = [NSString stringWithContentsOfFile:@"/Users/apple/Desktop/crayons.txt" encoding:NSUTF8StringEncoding error:nil]; 7 // NSArray *colors = [allColorString componentsSeparatedByString:@"\n"]; 8 // NSLog(@"%@",[[colors objectAtIndex:0] componentsSeparatedByString:@" #"]); 9 10 11 //联系人管理器12 ContactsManager *cm = [[ContactsManager alloc] init];13 //联系人114 Contact *mohang = [[Contact alloc] initWithName:@"Mohan" phone:@"13925082699" sex:@"男" age:22];15 //联系人216 Contact *mochou = [[Contact alloc] initWithName:@"Mochou" phone:@"13854389438" sex:@"女" age:20];17 //联系人318 Contact *moyan = [[Contact alloc] initWithName:@"Moyan" phone:@"18100000000" sex:@"男" age:46];19 20 //联系人421 Contact *yangbailao = [[Contact alloc] initWithName:@"Yangbailao" phone:@"13944774747" sex:@"男" age:56];22 //联系人523 Contact *yangxier = [[Contact alloc] initWithName:@"Yangxier" phone:@"13837373737" sex:@"女" age:18];24 //联系人625 Contact *panjinlian = [[Contact alloc] initWithName:@"Panjinlian" phone:@"18188888888" sex:@"女" age:27];26 27 //联系人128 Contact *caocao = [[Contact alloc] initWithName:@"Caocao" phone:@"1811111111" sex:@"男" age:47];29 //联系人230 Contact *caozhi = [[Contact alloc] initWithName:@"Caozhi" phone:@"13274747474" sex:@"男" age:16];31 //联系人332 Contact *caopi = [[Contact alloc] initWithName:@"Caopi" phone:@"18699995555" sex:@"男" age:34];33 34 [cm addContact:mohang];35 [cm addContact:mochou];36 [cm addContact:moyan];37 [cm addContact:panjinlian];38 [cm addContact:yangbailao];39 [cm addContact:yangxier];40 [cm addContact:caocao];41 [cm addContact:caopi];42 [cm addContact:caozhi];43 44 // [cm displayAllContacts];45 46 // [cm removeContactForName:@"Mohang"];47 // [cm removeContactForName:@"Moyan"];48 //49 //// [[cm contactForPhone:@"18188888888"] displayInfo];50 //// NSLog(@"---");51 //// NSArray *mContacts = [cm contactsForSex:@"女"];52 // NSArray *mContacts = [cm contactsForGroupName:@"M"];53 //54 //// NSLog(@"%@",mContacts);55 // for (Contact *c in mContacts) {56 // [c displayInfo];57 // }58 //// [cm removeContactForName:@"Haojianming"];59 60 return 0;61 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。