标签:interface address import 电话 字母
首先要创建 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
本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1543309
用OC写一个可以分字母组,的电话薄,并实现功能,布布扣,bubuko.com
标签:interface address import 电话 字母
原文地址:http://liuyafang.blog.51cto.com/8837978/1543309