OC-通讯录
要求描述:用OC语言完成简易通讯录(实现增删改查)功能.(注:使用MRC)
1.创建AddressBook类.
1)使用字典作为容器,字典的Key值为分组名(姓名首字母),value值为数组,数组中存放联系人(Person实例).(5分)
2)添加联系人.要求(20分)
a.判断姓名和电话不能为空否则添加失败.
b.判断是否已经存在分组,存在直接存储.不存在创建分组后再存储.(注:不能有 重名).
c.添加成功返回YES,姓名为空或者有重名的添加失败返回NO. d.数据使用Person存储.
e.方法:- (BOOL)addPerson:(Person *)aPerson;
3)按照姓名删除联系人.(15分)
a.方法:- (BOOL)deletePersonWithName:(NSString*)aPersonName; b.删除成功返回YES,否则返回NO;
4)删除一个分组.(10分)
a:方法:- (BOOL)deleteGroup:(NSString *)aGroupName;
b.删除成功返回YES,否则返回NO;
5)根据电话查找某个联系人.(10分)
a:方法:-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum;
b:如果找到返回该联系人,否则返回nil.
6)根据性别查找联系人,并将查到的联系人按照姓名排序(15分) a:方法:-(NSArray *)findPersonsWithSex:(NSString *)aSex; b:查找到联系人放入数组,排序的时候使用block.
7)根据姓名,更改联系人信息.(10分)
a:方法:-(BOOL)changePersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger) aAge; b:找到该联系人并修改该成功返回YES,否则返回NO
8)展示通讯录中的所有联系人(5分)
a:方法:-(void)showAll;
2.使用Person类.(属性:姓名,年龄,性别,电话)
1)重写description方法.(5分)
2)使用便利构造器.(5分)
一、main.m
#import <Foundation/Foundation.h> #import "Person.h" #import "AddressBook.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p1 = [Person personWithName:@"Mike" phoneNum:@"123" sex:@"m" age:12]; Person *p2 = [Person personWithName:@"Joe" phoneNum:@"234" sex:@"m" age:13]; Person *p3 = [Person personWithName:@"Kitty" phoneNum:@"345" sex:@"n" age:15]; Person *p5 = [Person personWithName:@"Andy" phoneNum:@"456" sex:@"n" age:15]; AddressBook *a = [[AddressBook alloc] init]; // 添加联系人 BOOL b1 = [a addPerson:p1]; BOOL b2 = [a addPerson:p2]; BOOL b3 = [a addPerson:p3]; BOOL b6 = [a addPerson:p5]; NSLog(@"%d %d %d %d",b1,b2,b3,b6); [a showAll]; // 按照姓名删除联系人 BOOL b4 = [a deletePersonWithName:@"Mike"]; NSLog(@"%d",b4); // 删除一个分组 BOOL b5 = [a deleteGroup:@"J"]; NSLog(@"%d",b5); // 根据电话查找某个联系人 Person *p4 = [a findPersonWithPhoneNum:@"345"]; NSLog(@"%@",p4); // 根据性别查找某个联系人,并将查到的联系人按照姓名排序 NSArray *arr = [a findPersonWithSex:@"n"]; for (Person *tempPerson in arr) { NSLog(@"----%@",tempPerson); } // 根据姓名,更改联系人信息 BOOL bb = [a changPersonWithName:@"Kitty" phoneNum:@"123" sex:@"n" age:15]; NSLog(@"%d",bb); NSLog(@"%@",a.addressBook); [a showAll]; [a autorelease]; } return 0; }
二、Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *phoneNum; @property (nonatomic,copy)NSString *sex; @property (nonatomic,assign)NSUInteger age; // 初始化方法 - (instancetype)initWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge; // 便利构造器 + (instancetype)personWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge; @end
三、Person.m
#import "Person.h" @implementation Person // 初始化方法 - (instancetype)initWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge{ if (self = [super init]) { self.name = name; self.phoneNum = aNum; self.sex = aSex; self.age = aAge; } return self; } // 便利构造器 + (instancetype)personWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge{ Person *p = [[Person alloc] initWithName:name phoneNum:aNum sex:aSex age:aAge]; return [p autorelease]; } // 重写description方法 - (NSString *)description{ return [NSString stringWithFormat:@"Name:%@ phoneNum:%@ sex:%@ age:%ld",self.name,self.phoneNum,self.sex,self.age]; } @end
四、AddressBook.h
#import <Foundation/Foundation.h> @class Person; @interface AddressBook : NSObject @property (nonatomic,retain)NSMutableDictionary *addressBook; // 获取首字母 - (NSString *)firstChar:(NSString *)str; // 添加联系人 - (BOOL)addPerson:(Person *)aPerson; // 按照姓名删除联系人 - (BOOL)deletePersonWithName:(NSString *)aPersonName; // 删除一个分组 - (BOOL)deleteGroup:(NSString *)aGroupName; // 根据电话朝找某个联系人 - (Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum; // 根据性别查找某个联系人,并将查到的联系人按照姓名排序 - (NSArray *)findPersonWithSex:(NSString *)aSex; // 根据姓名,更改联系人信息 - (BOOL)changPersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge; // 展示所有联系人 - (void)showAll; @end
五、AddressBook.m
#import "AddressBook.h" #import "Person.h" @implementation AddressBook // 重写初始化方法 - (instancetype)init { self = [super init]; if (self) { self.addressBook = [NSMutableDictionary dictionary]; } return self; } // 获取首字母 - (NSString *)firstChar:(NSString *)str{ return [str substringWithRange:NSMakeRange(0, 1)]; } // 添加联系人 - (BOOL)addPerson:(Person *)aPerson{ BOOL flat = NO; if (aPerson.name.length != 0) { if (aPerson.phoneNum.length != 0) { NSString *firstC = [self firstChar:aPerson.name]; if (self.addressBook[firstC] != nil) { for (Person *tempPerson in self.addressBook[firstC]) { if ([tempPerson.name isEqualToString:aPerson.name]) { flat = NO; break; }else{ flat = YES; } } if (flat == YES) { [self.addressBook[firstC] addObject:aPerson]; } }else{ NSMutableArray *tempArray = [NSMutableArray arrayWithObject:aPerson]; [self.addressBook setObject:tempArray forKey:firstC]; flat = YES; } } } return flat; } // 按照姓名删除联系人 - (BOOL)deletePersonWithName:(NSString *)aPersonName{ BOOL flat = NO; if (aPersonName.length != 0) { NSString *firstC = [self firstChar:aPersonName]; if (self.addressBook[firstC] != nil) { for (Person *tempPerson in self.addressBook[firstC]) { if ([tempPerson.name isEqualToString:aPersonName]) { flat = YES; if ([self.addressBook[firstC] count] == 1) { [self.addressBook removeObjectForKey:firstC]; }else{ [self.addressBook[firstC] removeObject:tempPerson]; } } } } } return flat; } // 删除一个分组 - (BOOL)deleteGroup:(NSString *)aGroupName{ BOOL flat = NO; if (aGroupName.length != 0) { if (self.addressBook[aGroupName] != nil) { for (NSString *tempKey in self.addressBook) { if ([tempKey isEqualToString:aGroupName]) { flat = YES; [self.addressBook removeObjectForKey:tempKey]; break; } } } } return flat; } // 根据电话朝找某个联系人 - (Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum{ if (aPhoneNum.length != 0) { BOOL flat = NO; Person *p = [[Person alloc] init]; [p autorelease]; NSArray *allValue = [self.addressBook allValues]; if (allValue != nil) { for (NSArray *tempArray in allValue) { for (Person *tempPerson in tempArray) { if ([tempPerson.phoneNum isEqualToString:aPhoneNum]) { flat = YES; p = tempPerson; break; } } } if (flat == YES) { return p; }else{ return nil; } }else{ return nil; } }else{ return nil; } } // 根据性别查找某个联系人,并将查到的联系人按照姓名排序 - (NSArray *)findPersonWithSex:(NSString *)aSex{ if (aSex.length != 0) { BOOL flat = NO; NSArray *allValue = [self.addressBook allValues]; NSMutableArray *receiveArray = [NSMutableArray array]; // [receiveArray autorelease]; if (allValue != nil) { for (NSArray *tempArray in allValue) { for (Person *tempPerson in tempArray) { if ([aSex isEqualToString:tempPerson.sex]) { flat = YES; [receiveArray addObject:tempPerson]; } } } if (flat == YES) { NSSortDescriptor *arrayAsceding = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *reternArray = [receiveArray sortedArrayUsingDescriptors:@[arrayAsceding]]; return [reternArray autorelease]; }else{ return nil; } }else{ return nil; } }else{ return nil; } } // 根据姓名,更改联系人信息 - (BOOL)changPersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge{ BOOL flat = NO; if (name.length != 0) { NSString *firstC = [self firstChar:name]; if (self.addressBook[firstC] != nil) { for (Person *tempPerson in self.addressBook[firstC]) { if ([tempPerson.name isEqualToString:name]) { flat = YES; tempPerson.phoneNum = aNum; tempPerson.sex = aSex; tempPerson.age = aAge; break; } } } } return flat; } // 展示所有联系人 - (void)showAll{ for (NSString *tempKey in self.addressBook) { for (Person *tempPerson in self.addressBook[tempKey]) { NSLog(@"%@",tempPerson); } } }