需求实现:

一、定义联系⼈类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.这个例子所用到的知识还是要清除数组/字典里面的构造和嵌套.

OC-字典&数组运用实例:通讯录的实现的更多相关文章

  1. OC ---- 字典集合 iOS学习-----细碎知识点总结

    实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:", @"age" ...

  2. OC NSArray 数组

    # OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...

  3. JSONModel 嵌套字典数组 JSONModel nest NSDictionary NSArray

    JSONModel 嵌套字典数组  JSONModel nest NSDictionary NSArray

  4. php数组去重实例及分析

    php数组去重实例及分析.  一维数组的重复项: 使用array_unique函数即可,使用实例 <?php    $aa=array("apple","banan ...

  5. JS中遍历普通数组和字典数组的区别

    // 普通数组 var intArray = new Array(); intArray[0] = "第一个"; intArray[1] = "第二个"; fo ...

  6. KVC 和 OC字典

    KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCo ...

  7. php array 根据value获取key,in_array()判断是否在数组内实例

    php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...

  8. swift和OC - 拆分数组 和 拆分字符串

    1. 拆分数组 /// 根据 数组 截取 指定个数返回 多个数组的集合 func splitArray( array: [Date], withSubSize subSize: Int) -> ...

  9. iOS_字典数组 按key分组和排序

    int main(int argc, const charchar * argv[]) { @autoreleasepool { // 1.定义一个测试的字典数组 NSMutableArray *di ...

随机推荐

  1. mongo export import

    mongoexport -h 127.0.0.1 -p 27170 -d dbs -c table -f field1,field2,field3 -q condition 路径.名称 mongoim ...

  2. C#。5 函数 类

    函数:能够独立完成某个功能的模块.          好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发.          四要素:名称,输入(参数),输出(返回的类型),加工(函 ...

  3. easyui-form添加自定义表单验证

    easyui自定义表单验证规则其实不是很复杂,只要重写一下重写 $.fn.validatebox.defaults.rules 自定义示例 $.extend($.fn.validatebox.defa ...

  4. firefox 不能显示 glyphicons 字体

    折腾了很久才发现是firefox 不能跨域下载相应的字体文件,将bootstrap相应的css文件和字体文件copy到调用的项目里,问题才得以解决.

  5. C#(Net)软件开发常用工具汇总,提高你的开发效率

    C#(Net)软件开发常用工具汇总,提高你的开发效率 写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用技术文章. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是 ...

  6. MVC---404页面配置

    参考地址1:http://benfoster.io/blog/aspnet-mvc-custom-error-pages 参考地址2:https://msdn.microsoft.com/en-us/ ...

  7. 你好,C++(19)“老师,我这次四级考试过了没有?”——4.2 条件选择语句

    4.2  条件选择语句 “老师,我这次四级考试过了没有?” 如果老师被问到这个问题,他会如何回答?是的,他会根据不同的条件选择不同的回答: 如果考试成绩大于等于60,那就回答:“恭喜你,你通过了这次考 ...

  8. (原)torch中微调某层参数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221664.html 参考网址: https://github.com/torch/nn/issues ...

  9. arm+linux 裸机环境搭建之初试minicom+dnw烧写uboot

    下面的步骤将会实现在linux下面使用dnw+minicom来烧写uboot 一.安装minicom 下载地址:http://download.csdn.net/detail/king_bingge/ ...

  10. keypress和keydown事件及keyCode和keyChar

    二:keypress和keydown事件: 现只在IE10.chrome(版本 32.0.1700.107 m).firefox(25.0.1)中测试了.   IE8 chrome firefox $ ...