OC-字典&数组运用实例:通讯录的实现

需求实现:

一、定义联系?类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] > 0 ) {
        NSString * firstname = [name substringToIndex:1];
        _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:1];
    if (self) {
        for (int i = 65; i < 91 ; i++) {
            NSString * key = [NSString stringWithFormat:@"%c",i];
            [_dic setValue:[[NSMutableArray alloc] initWithCapacity:1] forKey:key];
        }

    }
//    NSLog(@"%@",_dic);
    return self;
}

- (BOOL)addPerson:(ContactPerson *)person
{
    BOOL isAdd = NO;
    if ([[person name] length] == 0 || [[person phonenumber] length] == 0) {
        NSLog(@"姓名或者电话号码为空!   添加失败!");
    }
    else
    {
        for (int i = 0; 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] == 0) {
        NSLog(@"空号码!");
        return nil;
    }
    else
    {
        for (int i = 0; i < [_dic count]; i++) {
            NSString * key = [[_dic allKeys] objectAtIndex:i];
            for (int j = 0; 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:1];
    if ([sex length] == 0) {
        NSLog(@"性别为空");
    }
    else
    {
        for (int i = 0; i < [_dic count]; i ++) {
            NSString * key = [[_dic allKeys] objectAtIndex:i];
            for (int j = 0; 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] == 0) {
        NSLog(@"姓名为空");
        return isDelete;
    }
    for (int i = 0; i < [_dic count]; i++) {
        NSString * key = [[_dic allKeys] objectAtIndex:i];
        for (int j = 0; 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] == 0) {
        NSLog(@"组名为空");

    }
    for (int i = 0; i < [_dic count]; i++) {
        NSString * key = [[_dic allKeys] objectAtIndex:i];
        for (int j = 0; 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:@"13232323211" adress:@"莲花街11号"];
        ContactPerson * person2 = [[ContactPerson alloc] initWithName:@"zhangsaisai" sex:@"男" phonenumber:@"18637732345" adress:@"莲花街22号"];
        ContactPerson * person3 = [[ContactPerson alloc] initWithName:@"zhouwenshuai" sex:@"男" phonenumber:@"18534229999" adress:@"莲花街33号"];
        ContactPerson * person4 = [[ContactPerson alloc] initWithName:@"qinbingsen" sex:@"女" phonenumber:@"18123445235" adress:@"莲花街44号"];
        ContactPerson * person5 = [[ContactPerson alloc] initWithName:@"zhaoxiaolong" sex:@"男" phonenumber:@"18243528999" adress:@"莲花街55号"];
        ContactPerson * person6 = [[ContactPerson alloc] initWithName:@"chenglong" sex:@"女" phonenumber:@"13823342211" 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:@"13823342211"];
        NSLog(@"%@",p);
        NSLog(@"*******通过电话号码查询  123456*******");
        ContactPerson * p1 = [book searchPersonByphoneNumber:@"123456"];
        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 0;
}

main.m

重点:

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

//重写init方法,初始化字典
- (id)init
{
    self = [super init];
    _dic = [NSMutableDictionary dictionaryWithCapacity:1];
    if (self) {
        for (int i = 65; i < 91 ; i++) {
            NSString * key = [NSString stringWithFormat:@"%c",i];
            [_dic setValue:[[NSMutableArray alloc] initWithCapacity:1] 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.这个例子所用到的知识还是要清除数组/字典里面的构造和嵌套.

时间: 2024-10-27 10:32:52

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

Swift的数组与OC中数组的区别

相同的值可以多次出现在一个数组的不同位置: Swift中的数组,数据值在被存储进入到某个数组之前类型必须明确,可以显示的类型标注或者类型推断.而且,Swift中的数组不必是对象类型. OC中的NSArray和NSMutableArray,他们可以存储任何类型的实例,而且不提供他们返回对象的任何本质信息. Swift的数组与OC中数组的区别,布布扣,bubuko.com

OC 字典 集合

用字典能有什么好处? 字典是个大容器,它能够储存多个数据 用字典存储的数据具有一一对应的关系(使用key来标识value) 字典中一对键值对(key-value)叫做字典中的一个元素,也叫一个条目,只要是对象就可以,不限制类型 字典是无序的 字典中的key是唯一的,一个key只能对应一个value,一个value可以对应多个key 创建字典对象: 1.便利构造器(+号方法): NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKe

字符串,字典,数组写入本地文件和从本地文件读取

参考:http://blog.csdn.net/hakusan/article/details/39429393?utm_source=tuicool&utm_medium=referral 一.字符串,字典,数组存储到本地文件 字符串,数组,字典存储到本地文件过程一样,只是要存储的数据类型不同而已,这里以字符串存储到本地文件为例,如下:    NSString *content = @"将字符串存储到本地文件";    (1)获取Documents文件夹路径 参数:(1)指定

JSP简单练习-数组应用实例

<%@ page contentType="text/html; charset=gb2312" %> <html> <body> <% int i,j,k; // 声明一个5行6列的二维数组 int intArray[][]=new int[5][6]; k=0; // 数组赋初始值 for(i=0;i<5;i++) { for(j=0;j<6;j++) { intArray[i][j]=k; k++; } } // 输出数组中的

KVC 和 OC字典

KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCopying>)#>] int main(int argc, const charchar * argv[]) { @autoreleasepool { NSMutableDictionary *dict=[[NSMutableDictionary alloc] init]; [dict setOb

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

实例方法的创建 NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:@"悟空", @"name", @"男", @"gender", @"500", @"age", nil ]; NSLog(@"%@", wukong); // 便利构造器创建 NSDictionary *wuNeng

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

// 普通数组 var intArray = new Array(); intArray[0] = "第一个"; intArray[1] = "第二个"; for(var i = 0; i < intArray.length;i++) { alert(intArray[i]);    // 第一个,第二个 } // 拿到的是下标(像dictionary的key) for(var key in intArray) { alert(key);       // 0

JSON 数组转化 OC的数组转化成OC格式的字符串

1.将OC的数组转化成OC格式的字符串    NSArray * arr = @[@"aa",@"bb",@"cc"];2.转化    NSString * jsonStr = [arr JSONString];    NSLog(@"%@",jsonStr);

字典数组转为模型数组

在控制器上懒加载 - (NSArray *)statuses { if (_statuses == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMu