// Created By 郭仔 2015年04月09日21:27:50
经过两天的低迷,状态在慢慢的回归了,生活还要继续,人生还需奋斗!
祝:好人一生平安!!!
========================================================================
题目描述:
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; c.当删除联系?人后分组为空时,需要删除该分组。
4)删除?一个分组.(10分)
a:?方法:- (BOOL)deleteGroup:(NSString *)aGroupName; b.删除成功返回YES,否则返回NO;
5)根据电话查找某个联系?人.(10分)
a:?方法:-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum; b:如果找到返回该联系?人,否则返回nil. c.忽略电话重复问题,只返回第?一个查找到的联系?人。
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
c.修改电话、性别、年龄,参数不能为空。
8)展?示通讯录中的所有联系?人信息(5分) a:?方法:-(void)showAll;
b.通过重写description?方法展?示联系?人信息 。
2.使?用Person类.(属性:姓名,年龄,性别,电话,分组) 1)分组是姓名?首字?母(?大写),使?用提供的NSString类??目中的?方法获取。 2)重写description?方法,输出“姓名:张三 性别:男 年龄:22 电话:
123456789”.(5分) 3)定义Person类的便利构造器?方法,?方法包含参数:姓名、年龄、性别、电话.(5分)
3.main函数中的实现 1).创建?一个AddressBook类的实例(通讯录).
2).使?用便利构造器创建四个Person类的实例(联系?人). 姓名:Lucy 性别:?女 电话:123 年龄:21
姓名:Joe 性别:男 电话:456 年龄:17
姓名:Baby 性别:?女 电话:789 年龄:27
姓名:Linda 性别:?女 电话:000 年龄:21 3).调?用添加联系?人?方法,将四个联系?人添加到通讯录中.? 4).根据电话查找联系?人,并打印(查找电话:456); 5).根据性别查找联系?人,并将查到的联系?人按照年龄升序排序,并打印(查找性别:?女) 6).根据姓名,更改联系?人信息.(修改Linda的信息: 性别:?女 电话:012 年龄:25); 7).按照姓名删除联系?人,并展?示通信录中所有?人信息.(删除联系?人:Baby)
8).删除?一个分组,并展?示通信录中所有?人信息.(删除L组)
=========================================================================
AddressBook.h:
<span style="font-size:18px;">- (BOOL)addPerson:(Person *)aPerson; - (BOOL)isGroupExist:(NSString *)groupName; - (Person *)selectPersonWithName:(NSString *)name; - (BOOL)deletePersonWithName:(NSString *)aPersonName; - (BOOL)deleteGroup:(NSString *)aGroupName; - (Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum; - (NSArray *)findPersonsWithSex:(NSString *)aSex; - (BOOL)changePersonWithName:(NSString *)name phone:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge; - (void)showAll;</span>
AddressBook.m:
<span style="color:#333333;"><span style="font-size: 18px;">// 根据姓名查找对应的人 </span><span style="font-size:18px;">- (Person *)selectPersonWithName:(NSString *)name { NSString * grup = [name uppercasePinYinFirstLetter]; BOOL isGupExist = [self isGroupExist:grup]; if (!isGupExist) { return nil; } else{ NSMutableArray * arr = [_dic objectForKey:grup]; for (Person * per in arr) { if ([per.name isEqualToString:name]) { return per; } } } return nil; } // 判断是否存在guoupName组 - (BOOL)isGroupExist:(NSString *)groupName { for (NSString * key in _dic) { if ([key isEqualToString:groupName]) { return YES; } } return NO; } // 添加联系人 - (BOOL)addPerson:(Person *)aPerson { Person * per = [self selectPersonWithName:aPerson.name]; if (per) { NSLog(@"The Person is Exist!"); return NO; } BOOL isGroupExt = [self isGroupExist:aPerson.group]; if (!isGroupExt) { NSMutableArray *arr = [NSMutableArray array]; [_dic setObject:arr forKey:aPerson.group]; [arr addObject:aPerson]; } else{ NSMutableArray * arr = [_dic objectForKey:aPerson.group]; [arr addObject:aPerson]; } return YES; } // 按照姓名删除联系人 - (BOOL)deletePersonWithName:(NSString *)aPersonName { Person * per = [self selectPersonWithName:aPersonName]; if (per == nil) { NSLog(@"The Person Is Not Exist!"); return NO; } NSMutableArray *arr = [_dic objectForKey:per.group]; [arr removeObject:per]; if (arr == nil) { // [arr count]==0 [_dic removeObjectForKey:per.group]; } return YES; } // 删除一个分组 - (BOOL)deleteGroup:(NSString *)aGroupName { BOOL isGroubExt = [self isGroupExist:aGroupName]; if (!isGroubExt) { NSLog(@"The Group Is Not Exist!"); return NO; } [_dic removeObjectForKey:aGroupName]; return YES; } // 根据电话查找某个联系人 - (Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum { for (NSString * key in _dic) { NSMutableArray * arr = [_dic objectForKey:key]; for (Person * per in arr) { if ([per.phone isEqualToString:aPhoneNum]) { return per; } } } return nil; } // 根据性别查找联系人,并将查到的联系人按照年龄的升序排序 - (NSArray *)findPersonsWithSex:(NSString *)aSex { NSMutableArray * result = [NSMutableArray array]; for (NSString *key in _dic) { NSMutableArray *arr = [_dic objectForKey:key]; for (Person *per in arr) { if ([per.sex isEqualToString:aSex]) { [result addObject:per]; } } } NSArray * arr1 = [result sortedArrayUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2) { if (obj1.age > obj2.age) { return NSOrderedDescending; } else if(obj1.age == obj2.age) { return NSOrderedSame; } else return NSOrderedAscending; }]; return arr1; } // 根据姓名,更改练习人信息 - (BOOL)changePersonWithName:(NSString *)name phone:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge { Person *per = [self selectPersonWithName:name]; if (!per) { NSLog(@"NO Person!"); return NO; } per.name = name; per.phone = aNum; per.sex = aSex; per.age = aAge; return YES; } // 展示通讯录中的所有联系人信息 - (void)showAll { for (NSString *key in _dic) { NSMutableArray * arr = [_dic objectForKey:key]; for (Person *per in arr) { NSLog(@"%@",per); } } } - (void)dealloc { [_dic release]; [super dealloc]; } // 单独给NSMutableDictiona类型的dic分配空间</span></span>
<span style="color:#333333;"><span style="font-size:18px;">// 也可在主函数中单独给dic分配NSMutableDictionary空间,这里就没用重定义了 - (instancetype)init { self = [super init]; if (self) { _dic = [[NSMutableDictionary alloc]init]; } return self; }</span><span style="font-size: 18px;"> </span></span>
这里用到了一个uppercasePinYinFirstLetter方法,用来取出NSString类型字符串的首字母并把它转化为大写,这是在NSString的分类(类目)中实现的。
我定义的分类是NSString+firstCapitalLetter,把这个类目当做模板来用,别的工程把该类目考入能直接用。
NSString+firstCapitalLetter.h中实现
<span style="font-size:18px;">#import <Foundation/Foundation.h> #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZ#" char pinyinFirstLetter(unsigned short hanzi); @interface NSString (firstCapitalLetter) // 提供首字母大写 - (NSString *) uppercasePinYinFirstLetter; - (NSString *) lowercasePinYinFirstLetter; @end</span>
NSString+firstCapitalLetter.m
#import "NSString+firstCapitalLetter.h" #define HANZI_START 19968 #define HANZI_COUNT 20902 static char firstLetterArray[HANZI_COUNT] = "ydkqsxnwzssxjbymgcczqpssqbycdscdqldylybssjgyqzjjfgcclzznwdwzjljpfyynnjjtmynzwzhflzppqhgccyynmjqyxxgd";
char pinyinFirstLetter(unsigned short hanzi) { int index = hanzi - HANZI_START; if (index >= 0 && index <= HANZI_COUNT) { return firstLetterArray[index]; } else { return hanzi; } } @implementation NSString(firstCapitalLetter) - (NSString *) uppercasePinYinFirstLetter { unichar firstLetter = [self characterAtIndex:0]; char cFirstLetter = pinyinFirstLetter(firstLetter); return [[NSString stringWithFormat:@"%c",cFirstLetter] uppercaseString]; } - (NSString *) lowercasePinYinFirstLetter { unichar firstLetter = [self characterAtIndex:0]; char cFirstLetter = pinyinFirstLetter(firstLetter); return [[NSString stringWithFormat:@"%c",cFirstLetter] lowercaseString]; } @end
模板用就行
==============================================
Person.h
<span style="font-size:14px;">@property(nonatomic,copy)NSString * name; @property(nonatomic,assign)NSInteger age; @property(nonatomic,copy) NSString * sex; @property(nonatomic,copy)NSString * phone; @property(nonatomic,copy)NSString * group; // person的便利构造器 + (instancetype)personWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex phone:(NSString *)phone; - (instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex phone:(NSString *)phone;</span>
Person.m
- (void)setName:(NSString *)name { if (_name != name) { [_name release]; _name = [name copy]; } self.group = [name uppercasePinYinFirstLetter]; }
最近事情有些多,有些伤心
- (NSString *)description { return [NSString stringWithFormat:@"name=%@,age=%ld,sex=%@,phone=%@",_name,_age,_sex,_phone]; } // + (instancetype)personWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex phone:(NSString *)phone { Person * per = [[Person alloc]initWithName:name age:age sex:sex phone:phone]; return [per autorelease]; } // - (instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex phone:(NSString *)phone { if (self = [super init]) { self.name = name; self.age = age; self.sex = sex; self.phone = phone; //================= self.group = [name uppercasePinYinFirstLetter]; } return self; }
=================================================================
// 此句只能给AddressBook中的dic分配了一个指针的空间大小 AddressBook * addressBook = [[AddressBook alloc]init]; // 单独给NSMutableDictionary类型的dic分配空间 // 如果不使用AddressBook.m的init方法则用这一句就可以替代它 addressBook.dic = [[NSMutableDictionary alloc] init]; Person * per1 = [Person personWithName:@"Lucy" age:21 sex:@"女" phone:@"123"]; Person * per2 = [Person personWithName:@"Joe" age:17 sex:@"男" phone:@"456"]; Person * per3 = [Person personWithName:@"Baby" age:27 sex:@"女" phone:@"789"]; Person * per4 = [Person personWithName:@"Linda" age:21 sex:@"女" phone:@"000"]; //======================== [addressBook addPerson:per1]; [addressBook addPerson:per2]; [addressBook addPerson:per3]; [addressBook addPerson:per4]; [addressBook showAll]; //======================== Person * p = [addressBook findPersonWithPhoneNum:@"456"]; if (!p) { NSLog(@"NULL!"); } else{ NSLog(@"%@",p); } //======================== NSArray * arr = [addressBook findPersonsWithSex:@"女"]; NSLog(@"%@",arr); //======================== [addressBook changePersonWithName:@"Linda" phone:@"012" sex:@"女" age:25]; NSLog(@"%@",[addressBook selectPersonWithName:@"Linda"]); //======================== [addressBook deletePersonWithName:@"Baby"]; [addressBook showAll]; // //======================== [addressBook deleteGroup:@"L"]; [addressBook showAll];[addressBook showAll];
==================================================================
人活在世,不仅仅为了自己,更多的是不要让那些关心你的人失望!!!!
-------郭仔