实现中等难度通讯录。需求:
1、定义联系⼈人类Contact。实例变量:姓名(拼⾳音,⾸首字⺟母⼤大写)、性别、电话号码、 住址、分组名称、年龄。⽅方法:⾃自定义初始化⽅方法(姓名、电话号码)、显⽰示联系⼈人信息
2、在main.m中定义字典,分组管理所有联系⼈人。分组名为26个⼤大写的英⽂文字⺟母。
3、可以添加联系⼈人对象,如果姓名或电话号码为空,添加失败。添加联系⼈人到匹配的分 组。
4、获取某个分组名称下所有联系⼈人,并且按照姓名升序排列。
5、从通讯录中根据电话号码搜索联系⼈人。
6、获取所有⼥女性的联系⼈人,并且按照年龄的降序排列。
7、根据姓名删除某个联系⼈人。
8、删除某个分组的全部联系⼈人。
Contact.h
#import
@interface Contact : NSObject
@property (nonatomic, retain)NSString *name;
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, retain)NSString *sex;
@property (nonatomic, retain)NSString *phoneNumber;
@property (nonatomic, retain)NSString *addr;
@property (nonatomic, retain)NSString *group;
- (id)initWithName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone;
+ (id)contactWithName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone
addr:(NSString *)addr;
- (NSComparisonResult)sortContactByAge:(Contact *)otherCont;
- (NSComparisonResult)sortContactByName:(Contact *)otherCont;
- (void)info;
@end
Contact.m
#import "Contact.h"
@implementation Contact
- (id)initWithName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone
{
self = [super init];
if (self) {
_name = name;
_age = age;
_sex = sex;
_phoneNumber = phone;
}
return self;
}
+ (id)contactWithName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone
addr:(NSString *)addr
{
Contact *con = [[Contact alloc]initWithName:name age:age sex:sex phoneNumber:phone];
con.addr = addr;
con.group = [[con.name substringToIndex:1]uppercaseString];
return con;
}
- (NSComparisonResult)sortContactByAge:(Contact *)otherCont
{
if (self.age > otherCont.age) {
return NSOrderedDescending;
}
else if (self.age < otherCont.age){
return NSOrderedAscending;
}
else
return NSOrderedSame;
}
- (NSComparisonResult)sortContactByName:(Contact *)otherCont
{
if ([self.name compare:otherCont.name] > 0) {
return NSOrderedAscending;
}
else if ([self.name compare:otherCont.name] < 0){
return NSOrderedDescending;
}
else
return NSOrderedSame;
}
- (void)info
{
NSLog(@"name:%@, age:%ld, sex:%@, phone:%@, addr:%@, group:%@", _name, _age, _sex, _phoneNumber, _addr, _group);
}
@end
AddressBook.h
//
// AddressBook.h
// 07_字典实现通讯录
//
// Created by zhangzhipeng on 15/4/15.
// Copyright (c) 2015年 www.lanou3g.com蓝欧3G. All rights reserved.
//
#import
#import "Contact.h"
@interface AddressBook : NSObject
@property (nonatomic, retain) NSMutableDictionary *conDic;
- (id)init;
- (void)addContactPersonName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone
addr:(NSString *)addr;
- (void)getAllContactByGroupAndSortByNameAsceding:(NSString*)group;
- (void)searchContactByPhoneNumber:(NSString *)phoneNumber;
- (void)getAllContactBySexAndSortByNameDescding:(NSString *)sex;
- (void)deleteContactByName:(NSString *)name;
- (void)deleteContactByGroup:(NSString *)group;
- (void)listAllContact;
@end
AddressBook.m
//
// AddressBook.m
// 07_字典实现通讯录
//
// Created by zhangzhipeng on 15/4/15.
// Copyright (c) 2015年 www.lanou3g.com蓝欧3G. All rights reserved.
//
#import "AddressBook.h"
@implementation AddressBook
- (id)init
{
self = [super init];
if (self) {
self.conDic = [NSMutableDictionary dictionary];
}
return self;
}
- (void)addContactPersonName:(NSString *)name
age:(NSInteger)age
sex:(NSString *)sex
phoneNumber:(NSString *)phone
addr:(NSString *)addr
{
if (name == nil || [name isEqualToString:@" "] || phone ==nil || [phone isEqualToString:@" "]) {
NSLog(@"增加联系人失败");
}
else
{
Contact *con = [Contact contactWithName:name age:age sex:sex phoneNumber:phone addr:addr];
NSMutableArray *conArr = [self.conDicvalueForKey:con.group];
if (conArr == nil) {
conArr = [NSMutableArray array];
[conArr addObject:con];
[self.conDic setObject:conArr forKey:con.group];
}
else{
[conArr addObject:con];
}
}
}
- (void)getAllContactByGroupAndSortByNameAsceding:(NSString*)group
{
NSMutableArray *mArr = [self.conDic valueForKey:group];
[mArr sortUsingSelector:@selector(sortContactByName:)];
for (Contact *cont in mArr) {
[cont info];
}
}
- (void)searchContactByPhoneNumber:(NSString *)phoneNumber
{
NSMutableArray *arrPhoneNumber = [NSMutableArray array];
for (NSString *key in self.conDic) {
NSMutableArray *mArr = [self.conDic objectForKey:key];
for (Contact *cont in mArr) {
if ([cont.phoneNumber isEqualToString:phoneNumber]) {
[arrPhoneNumber addObject:cont];
}
}
}
for (Contact *cont in arrPhoneNumber) {
[cont info];
}
}
- (void)getAllContactBySexAndSortByNameDescding:(NSString *)sex
{
NSMutableArray *arrSex = [NSMutableArray array];
for (NSString *key in self.conDic) {
NSMutableArray *arr = [self.conDic valueForKey:key];
for (Contact *cont in arr) {
if ([cont.sex isEqualToString:sex]) {
[arrSex addObject:cont];
}
}
}
[arrSex sortUsingSelector:@selector(sortContactByAge:)];
for (Contact *cont in arrSex) {
[cont info];
}
}
- (void)deleteContactByName:(NSString *)name
{
NSMutableArray *arrDelete = [NSMutableArray array];
for ( NSString *key in self.conDic) {
NSMutableArray *arr = [self.conDic valueForKey:key];
for (Contact *cont in arr) {
if ([cont.name isEqualToString:name]) {
[arrDelete addObject:cont];
}
}
[arr removeObjectsInArray:arrDelete];
}
}
- (void)deleteContactByGroup:(NSString *)group
{
[self.conDic removeObjectForKey:group];
}
- (void)listAllContact
{
for (NSString *key in self.conDic) {
NSMutableArray *mArr = [self.conDic valueForKey:key];
for (Contact *con in mArr) {
[con info];
}
}
}
@end
转自:http://blog.csdn.net/qq_31810357