// 封装添加,查找,删除联系人的方法,创建AddressBook来管理操作通讯录的方法。
// AddressContact.h
@interface AddressContact : NSObject
{
NSString*_name;
NSString*_gender;
NSString*_number;
NSString*_address;
NSString*_groupName;
NSInteger _age;
}
//属性
@property (nonatomic,retain)NSString*name;
@property (nonatomic,retain)NSString*gender;
@property (nonatomic,retain)NSString*number;
@property (nonatomic,retain)NSString*address;
@property (nonatomic,retain)NSString*groupName;
@property (nonatomic)NSInteger age;
//初始化
-(id)initWithName:(NSString*)name
Gender:(NSString*)gender
Number:(NSString*)number
Address:(NSString*)address
GroupName:(NSString*)groupName
Age:(NSInteger)age;
+(id)addresscontactWithName:(NSString*)name
Gender:(NSString*)gender
Number:(NSString*)number
Address:(NSString*)address
GroupName:(NSString*)groupName
Age:(NSInteger)age;
//按姓名排序
-(NSComparisonResult)sortByName:(AddressContact*)aContact;
//按年龄排序
-(NSComparisonResult)sortByAge:(AddressContact*)aContact;
-(NSString *)description;
@end
// AddressContact.m
@implementation AddressContact
@synthesize name=_name;
@synthesize gender=_gender;
@synthesize number=_number;
@synthesize address=_address;
@synthesize groupName=_groupName;
@synthesize age=_age;
//初始化
-(id)initWithName:(NSString*)name
Gender:(NSString*)gender
Number:(NSString*)number
Address:(NSString*)address
GroupName:(NSString*)groupName
Age:(NSInteger)age{
self=[super init];
if (self) {
_name=[name retain];
_gender=[gender retain];
_number=[number retain];
_address=[address retain];
_groupName=[groupName retain];
_age=age;
}
return self;
}
-(void)dealloc{
[_name release];
[_gender release];
[_address release];
[_groupName release];
[_number release];
[super dealloc];
}
+(id)addresscontactWithName:(NSString*)name
Gender:(NSString*)gender
Number:(NSString*)number
Address:(NSString*)address
GroupName:(NSString*)groupName
Age:(NSInteger)age{
AddressContact *add=[[AddressContact alloc]initWithName:nameGender:gender Number:number Address:address GroupName:groupNameAge:age];
return [add autorelease] ;
}
//按姓名排序
-(NSComparisonResult)sortByName:(AddressContact*)aContact{
return [_name compare:[aContact name]];
}
//按年龄排序
-(NSComparisonResult)sortByAge:(AddressContact*)aContact{
if (_age<[aContact age]) {
return NSOrderedDescending;
}else if(_age==[aContact age]){
return NSOrderedSame;
}else{
return NSOrderedAscending;
}
}
-(NSString *)description{
return [NSStringstringWithFormat:@"%@,%@,%@,%@,%@,%ld",_name,_gender,
_number,_address,
_groupName,_age];
}
@end
// AddressBook.h
//创建AdressBook 类用来管理 操作通讯录 的方法。
@interface AddressBook : NSObject
{
//管理 通讯录的人 的字典,它的Key 值就是分组的值,Value 是一个数组,存放相同分组的人。
NSMutableDictionary *_addressBook;
// 这个字典当做 AddressBook 类的一个实例变量,用来存储所有的联系人,这样就不用在main函数中建字典,封装的更好。
}
//属性(省去setter ,getter 方法)
@property (nonatomic,retain)NSMutableDictionary*addressBook;
//初始化
-(id)init;
//将联系人添加到_addressBook字典中
//1,添加联系人
-(void)addContact:(AddressContact*)aContact;
//2,获取某个分组的所有联系人,并按姓名生序排序。
-(NSArray*)contactsWithGroupName:(NSString*)gName;
//3,根据电话号码搜索联系人
-(AddressContact*)searchContactsByNumber:(NSString*)cNum;
//4,按性别找联系人,并按年龄降序排列
-(NSArray*)searchContactsByGender:(NSString*)cGender;
//5,根据姓名删除某个联系人。
-(NSMutableDictionary*)removeOneContact:(AddressContact*)oneContact;
//-(void)removeOneContact:(AddressContact*)oneContact;//无返回值
//6,删除某个分组的全部联系人。
-(NSMutableDictionary*)removeGroupContacts:(NSString*)cGName;
//-(void)removeGroupContacts:(NSString*)cGname;//无返回值
@end
// AddressBook.m
@implementation AddressBook
//将字典初始化
- (id)init{
self=[super init];
if (self) {
_addressBook=[NSMutableDictionary dictionary];
}
return self;
}
//1,添加联系人
-(void)addContact:(AddressContact*)aContact{
//判断名字,电话是否为空
if ((aContact.name==0)||(aContact.number==0)) {
NSLog(@"联系人添加失败");
}else{//截取首字母,当做Key值
NSString *first=[aContact.name substringToIndex:1];
//判断首字母所在分组是否存在
if ([_addressBook objectForKey:first]==nil) {
//通过Key值,在字典中找Value,如果找不到,说明该分组不存在,就创建可变数组,添加首字母相同的联系人到数组中
//添加对象
NSMutableArray*tempArr=[NSMutableArray arrayWithObject:aContact];//与下面代码等价
// NSMutableArray *tempArr=[NSMutableArray array];
// [tempArr addObject:aContact];
//把数组中的联系人添加到字典中,以首字母为Key,以可变数组为Value
[_addressBook setObject:tempArr forKey:first];
}else{
//分组存在时,直接添加。添加时,先通过首字母(Key值)在字典找到相应的数组,然后再把联系人添加到数组中。
[[_addressBook objectForKey:first] addObject:aContact];
}
}
}
//2,获取某个分组的所有联系人,并按姓名升序排序。
-(NSArray*)contactsWithGroupName:(NSString*)gName{
NSArray*temp=[_addressBook objectForKey:gName];
//排序,sortByName 方法已经在联系人类中实现
return [temp sortedArrayUsingSelector:@selector(sortByName:)];
}
//3,根据 电话号码搜索联系人
-(AddressContact*)searchContactsByNumber:(NSString*)cNum{
for (NSString*group in _addressBook) {
NSArray *arr=[_addressBook objectForKey:group];
for (AddressContact *p in arr) {
if (cNum==p.number) {
return p;
}
}
}
return nil;
}
//4,按性别找联系人,并按年龄降序排列
-(NSArray*)searchContactsByGender:(NSString*)cGender{
NSMutableArray *marr=[[NSMutableArray alloc]init];//数组必须用可变的
for (NSString*group in _addressBook) {
NSArray*arr=[_addressBook objectForKey:group];
for (AddressContact *p in arr){
if (cGender==p.gender) {
[marr addObject:p];
}
}
}
[marr sortUsingSelector:@selector(sortByAge:)];
return marr;
}
//5,根据姓名删除某个联系人。
-(NSMutableDictionary*)removeOneContact:(AddressContact*)oneContact{
//-(void)removeOneContact:(AddressContact*)oneContact{
NSMutableArray *marr=[NSMutableArray array];
for (NSString*oneName in _addressBook) {
NSMutableArray*arr=[_addressBook objectForKey:oneName];
for (AddressContact *p in arr) {
if (p.name ==oneContact.name) {
[marr addObject:p];
// [arr removeObject:p];
// break;
}
}
}
// 截取首字母
NSString *first=[oneContact.name substringToIndex:1];
for ( AddressContact* q in marr) {
[[_addressBook objectForKey:first] removeObject:q];
}
return _addressBook;
}
//6,删除某个分组的全部联系人。
-(NSMutableDictionary*)removeGroupContacts:(NSString*)cGName{
NSArray*temp=[_addressBook allKeys];//获取所有Key值
for (NSString *gName in temp) {
if ([gName isEqualToString:cGName]) {
// if (gName ==cGName) {
[_addressBook removeObjectForKey:gName];
}
}
return _addressBook;
}
@end
// main.m
int main(int argc, const char * argv[]) {
@autoreleasepool {
AddressBook *book=[[AddressBook alloc]init];
// book.addressBook=[NSMutableDictionary dictionary];//代替-init
AddressContact *p1=[[AddressContact alloc]initWithName:@"Davi" Gender:@"m" Number:@"008" Address:@"xian" GroupName:@"D" Age:18];
AddressContact *p2=[[AddressContact alloc]initWithName:@"Andy" Gender:@"f" Number:@"010" Address:@"zhengzhou" GroupName:@"A" Age:19];
AddressContact *p3=[[AddressContact alloc]initWithName:@"Anni" Gender:@"f" Number:@"001" Address:@"shanghai" GroupName:@"A" Age:24];
AddressContact *p4=[[AddressContact alloc]initWithName:@"Denis" Gender:@"m" Number:@"006" Address:@"luoyang" GroupName:@"D" Age:30];
AddressContact *p5=[[AddressContact alloc]initWithName:@"Cady" Gender:@"f" Number:@"003" Address:@"xinxiang" GroupName:@"C" Age:21];
AddressContact *p6=[[AddressContact alloc]initWithName:@"Alan" Gender:@"m" Number:@"009" Address:@"dengfeng" GroupName:@"A" Age:20];
//1,添加联系人
[book addContact:p1];
[book addContact:p2];
[book addContact:p3];
[book addContact:p4];
[book addContact:p5];
[book addContact:p6];
NSLog(@"%@",book.addressBook);
//2,获取某个分组的所有联系人,并按姓名升序排序。
NSArray *name=[book contactsWithGroupName:@"A"];
NSLog(@"%@",name);
//3,根据电话号码搜索联系人
AddressContact *cc=[book searchContactsByNumber:@"003"];
NSLog(@"%@",cc);
//4,按性别找联系人,并按年龄降序排列
NSArray *marrAge=[book searchContactsByGender:@"m"];
NSLog(@"%@",marrAge);
//5,根据姓名删除某个联系人。
NSLog(@"%@",book.addressBook);
NSMutableDictionary *d=[book removeOneContact:p3];
NSLog(@"%@",d);
//6,删除某个分组的全部联系人。
NSMutableDictionary *b=[book removeGroupContacts:@"A"];
NSLog(@"%@",b);
[book release];
[p1 release];
[p2 release];
[p3 release];
[p4 release];
[p5 release];
[p6 release];
}
return 0;
}