首先要创建 AddressContact 类,
#import <Foundation/Foundation.h> //---------AddressContact的.h文件 @interface AddressContact : NSObject { NSString *_name; NSString *_sex; NSString *_phone; NSString *_address; NSString *_groupName; NSInteger _age; } - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone; - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex address:(NSString *)address groupName:(NSString *)groupName age:(NSInteger)age; - (NSString *)groupName; - (NSString *)phone; - (NSString *)sex; - (NSString *)name; - (NSInteger)age; //输出信息, - (void)displayContactInfo; //比较年龄 - (NSComparisonResult)compareAge:(AddressContact *)addressContact; //比较姓名 - (NSComparisonResult)compareName:(AddressContact *)addressContact; @end
----
#import "AddressContact.h" //------AddressContact的.m文件 @implementation AddressContact - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone { return [self initWithName:name phone:phone sex:nil address:nil groupName:nil age:0]; } - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex address:(NSString *)address groupName:(NSString *)groupName age:(NSInteger)age { self = [super init]; if (self) { _name = name; _phone = phone; _sex = sex; _address = address; _groupName = groupName; _age = age; } return self; } - (NSString *)groupName { return _groupName; } - (NSString *)phone { return _phone; } - (NSString *)sex { return _sex; } - (NSString *)name { return _name; } - (NSInteger)age { return _age; } - (void)displayContactInfo { NSLog(@"%@,%ld",_name,_age); } //比较年龄 - (NSComparisonResult)compareAge:(AddressContact *)addressContact { NSInteger myAge = [self age]; NSInteger otherAge = [addressContact age]; if (myAge > otherAge) { return NSOrderedAscending; }else if(myAge < otherAge){ return NSOrderedDescending; }else{ return NSOrderedSame; } } //比较姓名 - (NSComparisonResult)compareName:(AddressContact *)addressContact{ NSString *myName = [self name]; NSString *otherName = [addressContact name]; NSComparisonResult result = [myName compare:otherName]; if (result == NSOrderedAscending) { return NSOrderedDescending; }else if(result == NSOrderedDescending){ return NSOrderedAscending; }else { return NSOrderedSame; } } @end
-----------
然后在创建ContactManager类。。。。。。。。。。。
#import <Foundation/Foundation.h> // ------ContactManager的.h文件 #import "AddressContact.h" @interface ContactManager : NSObject { NSMutableDictionary *_contactDict; } //添加联系人 - (void)addVContact:(AddressContact *)contact; //获取某个分组下的联系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName; //根据电话号码搜索联系人 - (AddressContact *)contactForPhoneNumber:(NSString *)phoneNumer; //获取所有女性联系人,按照年龄降序 - (NSArray *)contactForSex:(NSString *)sex; //根据姓名删除某个联系人 - (void)removeContactForName:(NSString *)name; //删除某个分组的全部联系人 -(void)removeContactForGroupName:(NSString *)groupName; @end
------
#import "ContactManager.h" //--------ContactManager的.m文件 @implementation ContactManager //添加联系人 - (void)addVContact:(AddressContact *)contact { NSString *groupName = [contact groupName]; NSMutableArray *array = [_contactDict objectForKey:groupName]; if (array == nil) { array = [NSMutableArray arrayWithCapacity:0]; [array addObject:contact]; [_contactDict setObject:array forKey:groupName]; }else{ [array addObject: contact]; } } //获取某个分组下的联系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName { NSMutableArray *array = [_contactDict objectForKey:groupName]; [array sortUsingSelector:@selector(compareName:)]; return array; } //根据电话号码搜索联系人 - (AddressContact *)contactForPhoneNumber:(NSString *)phoneNumer { for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact phone] isEqualToString:phoneNumer]) { return contact; } } } return nil; } //获取所有女性联系人,按照年龄降序 - (NSArray *)contactForSex:(NSString *)sex { NSMutableArray *sexArray = [NSMutableArray arrayWithCapacity:0]; for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact sex] isEqualToString:sex]) { [sexArray addObject:contact]; } } } [sexArray sortedArrayUsingSelector:@selector(compareAge:)]; return sexArray; } //根据姓名删除某个联系人 - (void)removeContactForName:(NSString *)name { for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact name] isEqualToString:name]) { [array removeObject:contact]; } } } } //删除某个分组的全部联系人 -(void)removeContactForGroupName:(NSString *)groupName { [_contactDict removeObjectForKey:groupName]; } @end
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。