需求实现:

一、定义联系⼈类ContactPerson

实例变量:姓名、性别、电话号码、住址、分组名称。

方法:初始化⽅方法(姓名、电话号码)、显⽰示联系⼈信息

二、定义AddressBook类, 封装以下功能, 类中定义一个实例变量可变数组,管理所有联系人, 通讯录具有以下功能:

1,  可以添加新联系⼈对象,如果姓名或电话号码为空,打印添加失败。

2、根据电话号码搜索联系⼈

3、获取所有某性联系⼈

4、根据姓名删除联系⼈

5、展⽰示通讯录中所有联系

6. 删除某分组的所有联系人

ContactPerson类的声明:用于声明人的对象

@interface ContactPerson : NSObject
//一、定义联系⼈类ContactPerson
//实例变量:姓名、性别、电话号码、住址、分组名称。
//方法:初始化⽅方法(姓名、电话号码)、显⽰示联系⼈信息
{
NSString * _name;
NSString * _sex;
NSString * _phonenumber;
NSString * _adress;
NSString * _group;
}
- (id)initWithName:(NSString *)name sex:(NSString *)sex phonenumber:(NSString *)phonenumber adress:(NSString *)adress;
- (NSString *)group;
- (NSString *)name;
- (NSString *)phonenumber;
- (NSString *)sex;
@end

ContactPerson

ContactPerson类的实现:1.初始化⽅方法 2.set/get 方法 3.重写%@函数

#import "ContactPerson.h"

@implementation ContactPerson

- (id)initWithName:(NSString *)name sex:(NSString *)sex phonenumber:(NSString *)phonenumber adress:(NSString *)adress
{
self = [super init];
if (self) {
_name = name;
_sex = sex;
_phonenumber = phonenumber;
_adress = adress;
}
//判断首字母,给组名称赋值
if ([name length] > ) {
NSString * firstname = [name substringToIndex:];
_group = [firstname uppercaseString];
}
// NSLog(@"%@",_group); return self;
}
- (NSString *)group
{
return _group;
}
- (NSString *)name
{
return _name;
}
- (NSString *)phonenumber
{
return _phonenumber;
}
- (NSString *)sex
{
return _sex;
} - (NSString *)description
{
return [NSString stringWithFormat:@"name:%@ sex:%@ phonenumber:%@ adress:%@ group:%@",_name,_sex,_phonenumber,_adress,_group];
}

ContactPerson.m

AddressBook类的声明:1,  可以添加新联系⼈对象,如果姓名或电话号码为空,打印添加失败。 2、根据电话号码搜索联系⼈ 3、获取所有某性联系⼈ 4、根据姓名删除联系⼈ 5、展⽰示通讯录中所有联系 6. 删除某分组的所有联系人

#import <Foundation/Foundation.h>
@class ContactPerson;
@interface AddressBook : NSObject
{
//创建一个字典存储数据
NSMutableDictionary * _dic;
}
- (BOOL)addPerson:(ContactPerson *)person;
- (ContactPerson *)searchPersonByphoneNumber:(NSString *)phonenumber;
- (NSArray *)searchPersonBysex:(NSString *)sex;
- (BOOL)deleteByname:(NSString *)name;
- (BOOL)deleteByGroup:(NSString *)group;
- (void)showAll; @end

AddressBook.h

AddressBook类实现:

#import "AddressBook.h"
#import "ContactPerson.h"
@implementation AddressBook //重写init方法,初始化字典
- (id)init
{
self = [super init];
_dic = [NSMutableDictionary dictionaryWithCapacity:];
if (self) {
for (int i = ; i < ; i++) {
NSString * key = [NSString stringWithFormat:@"%c",i];
[_dic setValue:[[NSMutableArray alloc] initWithCapacity:] forKey:key];
} }
// NSLog(@"%@",_dic);
return self;
} - (BOOL)addPerson:(ContactPerson *)person
{
BOOL isAdd = NO;
if ([[person name] length] == || [[person phonenumber] length] == ) {
NSLog(@"姓名或者电话号码为空! 添加失败!");
}
else
{
for (int i = ; i < [_dic count]; i++) {
NSString * key = [[_dic allKeys] objectAtIndex:i];
if ([[person group] isEqualToString:key]) {
[[_dic objectForKey:key] addObject:person];
isAdd = YES;
}
}
}
return isAdd;
}
- (ContactPerson *)searchPersonByphoneNumber:(NSString *)phonenumber
{
BOOL isExist = NO;
if ([phonenumber length] == ) {
NSLog(@"空号码!");
return nil;
}
else
{
for (int i = ; i < [_dic count]; i++) {
NSString * key = [[_dic allKeys] objectAtIndex:i];
for (int j = ; j < [[_dic objectForKey:key] count]; j++) {
if ([[[[_dic objectForKey:key] objectAtIndex:j] phonenumber] isEqualToString:phonenumber]) {
isExist = YES;
return [[_dic objectForKey:key] objectAtIndex:j]; }
}
}
}
if (isExist == NO) {
NSLog(@"电话号码不存在");
}
return nil; } - (NSArray *)searchPersonBysex:(NSString *)sex
{
NSMutableArray * personArray = [[NSMutableArray alloc] initWithCapacity:];
if ([sex length] == ) {
NSLog(@"性别为空");
}
else
{
for (int i = ; i < [_dic count]; i ++) {
NSString * key = [[_dic allKeys] objectAtIndex:i];
for (int j = ; j < [[_dic objectForKey:key] count]; j++) {
if ([[[[_dic objectForKey:key] objectAtIndex:j] sex] isEqualToString:sex]) {
[personArray addObject:[[_dic objectForKey:key] objectAtIndex:j]];
}
}
}
}
return [NSArray arrayWithArray:personArray];
} - (BOOL)deleteByname:(NSString *)name
{
BOOL isDelete = NO;
BOOL isExist = NO;
if ([name length] == ) {
NSLog(@"姓名为空");
return isDelete;
}
for (int i = ; i < [_dic count]; i++) {
NSString * key = [[_dic allKeys] objectAtIndex:i];
for (int j = ; j < [[_dic objectForKey:key] count]; j++) {
if ([[[[_dic objectForKey:key] objectAtIndex:j] name] isEqualToString:name]) {
[[_dic objectForKey:key] removeObjectAtIndex:j];
isDelete = YES;
isExist = YES;
}
}
}
if (isExist == NO) {
NSLog(@"此联系人不存在");
}
return isDelete;
}
- (BOOL)deleteByGroup:(NSString *)group
{
BOOL isDelete = NO;
BOOL isExist = NO;
if ([group length] == ) {
NSLog(@"组名为空"); }
for (int i = ; i < [_dic count]; i++) {
NSString * key = [[_dic allKeys] objectAtIndex:i];
for (int j = ; j < [[_dic objectForKey:key] count]; j++) {
if ([[[[_dic objectForKey:key] objectAtIndex:j] group] isEqualToString:group]) {
[[_dic objectForKey:key] removeObjectAtIndex:j];
j--;
isDelete = YES;
isExist = YES;
}
}
}
if (isExist == NO) {
NSLog(@"此组不存在");
}
return isDelete;
} - (void)showAll
{
NSLog(@"%@",_dic);
} @end

AddressBook.m

#import <Foundation/Foundation.h>
#import "ContactPerson.h"
#import "AddressBook.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
ContactPerson * person1 = [[ContactPerson alloc] initWithName:@"lixiaoming" sex:@"男" phonenumber:@"" adress:@"莲花街11号"];
ContactPerson * person2 = [[ContactPerson alloc] initWithName:@"zhangsaisai" sex:@"男" phonenumber:@"" adress:@"莲花街22号"];
ContactPerson * person3 = [[ContactPerson alloc] initWithName:@"zhouwenshuai" sex:@"男" phonenumber:@"" adress:@"莲花街33号"];
ContactPerson * person4 = [[ContactPerson alloc] initWithName:@"qinbingsen" sex:@"女" phonenumber:@"" adress:@"莲花街44号"];
ContactPerson * person5 = [[ContactPerson alloc] initWithName:@"zhaoxiaolong" sex:@"男" phonenumber:@"" adress:@"莲花街55号"];
ContactPerson * person6 = [[ContactPerson alloc] initWithName:@"chenglong" sex:@"女" phonenumber:@"" adress:@"莲花街66号"];
AddressBook * book = [[AddressBook alloc] init];
//添加联系人
[book addPerson:person1];
[book addPerson:person2];
[book addPerson:person3];
[book addPerson:person4];
[book addPerson:person5];
[book addPerson:person6];
// [book showAll];
//通过电话号码查询
NSLog(@"-----------------------------------");
NSLog(@"*******通过电话号码查询 13823342211*******");
ContactPerson * p = [book searchPersonByphoneNumber:@""];
NSLog(@"%@",p);
NSLog(@"*******通过电话号码查询 123456*******");
ContactPerson * p1 = [book searchPersonByphoneNumber:@""];
NSLog(@"%@",p1);
NSLog(@"-----------------------------------");
//查询某性别所有的联系人
NSLog(@"*******查询某性别所有的联系人 女*******");
NSArray * array = [book searchPersonBysex:@"女"];
NSLog(@"%@",array);
NSLog(@"-----------------------------------");
//根据姓名删除某联系人
NSLog(@"*******根据姓名删除某联系人 chenglong*******");
NSLog(@"%@",[book deleteByname:@"chenglong"] ? @"YES" : @"NO");
[book showAll];
NSLog(@"*******根据姓名删除某联系人 haha*******");
NSLog(@"%@",[book deleteByname:@"haha"] ? @"YES" : @"NO");
NSLog(@"-----------------------------------");
//删除某分组的所有联系人
NSLog(@"*******删除某分组的所有联系人 H*********");
NSLog(@"%@",[book deleteByGroup:@"H"] ? @"YES" : @"NO");
NSLog(@"*******删除某分组的所有联系人 Z*********");
NSLog(@"%@",[book deleteByGroup:@"Z"] ? @"YES" : @"NO");
[book showAll]; }
return ;
}

main.m

重点:

1.在类声明对象的时候,直接重写NSObject的init方法,在init方法中声明一个有组名称的字典.

//重写init方法,初始化字典
- (id)init
{
self = [super init];
_dic = [NSMutableDictionary dictionaryWithCapacity:];
if (self) {
for (int i = ; i < ; i++) {
NSString * key = [NSString stringWithFormat:@"%c",i];
[_dic setValue:[[NSMutableArray alloc] initWithCapacity:] forKey:key];
} }
// NSLog(@"%@",_dic);
return self;
}

2.所用到的API函数重点:(1)获得key值.(2)通过key值取value (3).

NSDictionary * dic = [[NSDictionary alloc] init];
NSArray * array1 = [dic allKeys];
//取出对应的key值
NSString * key = [[_dic allKeys] objectAtIndex:i];
再通过key值取出相应的value值与传入的值进行比对
[[[[_dic objectForKey:key] objectAtIndex:j] phonenumber] isEqualToString:phonenumber]

3.这个例子所用到的知识还是要清除数组/字典里面的构造和嵌套.

05-02 22:16