标签:
// Created By 郭仔 2015年04月09日21:27:50
经过两天的低迷,状态在慢慢的回归了,生活还要继续,人生还需奋斗!
祝:好人一生平安!!!
========================================================================
题目描述:
1.创建AddressBook类. 1)使?用字典作为容器,字典的Key值为分组名(姓名?首字?母,?大写),value值为数组,数组
中存放联系?人(Person实例).(5分) 2)添加联系?人.要求(20分)姓名: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
模板用就行
==============================================<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];==================================================================
人活在世,不仅仅为了自己,更多的是不要让那些关心你的人失望!!!!
-------郭仔
标签:
原文地址:http://blog.csdn.net/guoxianzhuang/article/details/44965813