用OC写一个可以分字母组,的电话薄,并实现功能-创新互联
首先要创建 AddressContact 类,
公司主营业务:网站制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出青秀免费做网站回馈大家。#import//---------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// ------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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:用OC写一个可以分字母组,的电话薄,并实现功能-创新互联
文章来源:http://lswzjz.com/article/diicgh.html