用OC写一个可以分字母组,的电话薄,并实现功能
首先要创建AddressContact 类,
#import<Foundation/Foundation.h>//---------AddressContact的.h文件@interfaceAddressContact:NSObject{NSString*_name;NSString*_sex;NSString*_phone;NSString*_address;NSString*_groupName;NSInteger_age;}-(instancetype)initWithName:(NSString*)namephone:(NSString*)phone;-(instancetype)initWithName:(NSString*)namephone:(NSString*)phonesex:(NSString*)sexaddress:(NSString*)addressgroupName:(NSString*)groupNameage:(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文件@implementationAddressContact-(instancetype)initWithName:(NSString*)namephone:(NSString*)phone{return[selfinitWithName:namephone:phonesex:niladdress:nilgroupName:nilage:0];}-(instancetype)initWithName:(NSString*)namephone:(NSString*)phonesex:(NSString*)sexaddress:(NSString*)addressgroupName:(NSString*)groupNameage:(NSInteger)age{self=[superinit];if(self){_name=name;_phone=phone;_sex=sex;_address=address;_groupName=groupName;_age=age;}returnself;}-(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{NSIntegermyAge=[selfage];NSIntegerotherAge=[addressContactage];if(myAge>otherAge){returnNSOrderedAscending;}elseif(myAge<otherAge){returnNSOrderedDescending;}else{returnNSOrderedSame;}}//比较姓名-(NSComparisonResult)compareName:(AddressContact*)addressContact{NSString*myName=[selfname];NSString*otherName=[addressContactname];NSComparisonResultresult=[myNamecompare:otherName];if(result==NSOrderedAscending){returnNSOrderedDescending;}elseif(result==NSOrderedDescending){returnNSOrderedAscending;}else{returnNSOrderedSame;}}@end
-----------
然后在创建ContactManager类。。。。。。。。。。。
#import<Foundation/Foundation.h>//------ContactManager的.h文件#import"AddressContact.h"@interfaceContactManager: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文件@implementationContactManager//添加联系人-(void)addVContact:(AddressContact*)contact{NSString*groupName=[contactgroupName];NSMutableArray*array=[_contactDictobjectForKey:groupName];if(array==nil){array=[NSMutableArrayarrayWithCapacity:0];[arrayaddObject:contact];[_contactDictsetObject:arrayforKey:groupName];}else{[arrayaddObject:contact];}}//获取某个分组下的联系人。-(NSArray*)allContactsForGroupName:(NSString*)groupName{NSMutableArray*array=[_contactDictobjectForKey:groupName];[arraysortUsingSelector:@selector(compareName:)];returnarray;}//根据电话号码搜索联系人-(AddressContact*)contactForPhoneNumber:(NSString*)phoneNumer{for(NSString*keyin_contactDict){NSMutableArray*array=[_contactDictobjectForKey:key];for(AddressContact*contactinarray){if([[contactphone]isEqualToString:phoneNumer]){returncontact;}}}returnnil;}//获取所有女性联系人,按照年龄降序-(NSArray*)contactForSex:(NSString*)sex{NSMutableArray*sexArray=[NSMutableArrayarrayWithCapacity:0];for(NSString*keyin_contactDict){NSMutableArray*array=[_contactDictobjectForKey:key];for(AddressContact*contactinarray){if([[contactsex]isEqualToString:sex]){[sexArrayaddObject:contact];}}}[sexArraysortedArrayUsingSelector:@selector(compareAge:)];returnsexArray;}//根据姓名删除某个联系人-(void)removeContactForName:(NSString*)name{for(NSString*keyin_contactDict){NSMutableArray*array=[_contactDictobjectForKey:key];for(AddressContact*contactinarray){if([[contactname]isEqualToString:name]){[arrayremoveObject:contact];}}}}//删除某个分组的全部联系人-(void)removeContactForGroupName:(NSString*)groupName{[_contactDictremoveObjectForKey:groupName];}@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。