码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-15 16:31:03      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

需求实现:

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

 

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

标签:

原文地址:http://www.cnblogs.com/IT-jqm/p/NSDictionary_IT_JQM.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!