OC 字典 存储联系人信息 求大神优化代码

1、定义联系⼈人类Contact。实例变量:姓名(拼⾳音,⾸首字⺟母⼤大写)、性别、电话号码、 住址、分组名称、年龄。⽅方法:⾃自定义初始化⽅方法(姓名、电话号码)、显⽰示联系⼈人信息


2、在main.m中定义字典,分组管理所有联系⼈人。分组名为26个⼤大写的英⽂文字⺟母。


 3、可以添加联系⼈人对象,如果姓名或电话号码为空,添加失败。添加联系⼈人到匹配的分 组。


4、获取某个分组名称下所有联系⼈人,并且按照姓名升序排列。


5、从通讯录中根据电话号码搜索联系⼈人。


6、获取所有⼥女性的联系⼈人,并且按照年龄的降序排列。


7、根据姓名删除某个联系⼈人。
 8、删除某个分组的全部联系⼈人。 

 

.H文件

Contact.h

#import <Foundation/Foundation.h>

 

@interface Contact : NSObject

@property (nonatomic, retain)NSString *name;

@property (nonatomic, retain)NSString *sex;

@property (nonatomic, assign)NSInteger age;

@property (nonatomic, retain)NSString *addrs;

@property (nonatomic, retain)NSString *phone;

@property (nonatomic, retain)NSString *group;

- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age adds:(NSString *)adds phone:(NSString *)phone group:(NSString *)group;

@end

实现文件.M

#import "Contact.h"

@implementation Contact

- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age adds:(NSString *)adds phone:(NSString *)phone group:(NSString *)group

{

    self = [self init];

    if (self) {

        [self setName:name];

        [self setSex:sex];

        [self setAge:age];

        [self setAddrs:adds];

        [self setPhone:phone];

        [self setGroup:group];

    }

    return self;

}

- (NSString *)description

{

    return [NSString stringWithFormat:@"name:%@, phone:%@, group:%@", self.name, self.phone, self.group];

}

@end

 

MAIN () 函数

// 1、定义联系⼈类Contact。实例变量:姓名(拼音,⾸字母大写)、性别、电话号码、di址、分组名称、年龄。方法:⾃定义初始化方法(姓名、电话号码)、显⽰示联系人信息

// 2.在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英⽂字母。

 

// 定义数组 存储联系人对象

    NSMutableArray *arr0 = [NSMutableArray array];

    NSMutableArray *arr1 = [NSMutableArray array];

    NSMutableArray *arr2 = [NSMutableArray array];

    NSMutableArray *arr3 = [NSMutableArray array];

    NSMutableArray *arr4 = [NSMutableArray array];

    NSMutableArray *arr5 = [NSMutableArray array];

    NSMutableArray *arr6 = [NSMutableArray array];

    NSMutableArray *arr7 = [NSMutableArray array];

    NSMutableArray *arr8 = [NSMutableArray array];

    NSMutableArray *arr9 = [NSMutableArray array];

    NSMutableArray *arr10 = [NSMutableArray array];

    NSMutableArray *arr11 = [NSMutableArray array];

    NSMutableArray *arr12 = [NSMutableArray array];

    NSMutableArray *arr13 = [NSMutableArray array];

    NSMutableArray *arr14 = [NSMutableArray array];

    NSMutableArray *arr15 = [NSMutableArray array];

    NSMutableArray *arr16 = [NSMutableArray array];

    NSMutableArray *arr17 = [NSMutableArray array];

    NSMutableArray *arr18 = [NSMutableArray array];

    NSMutableArray *arr19 = [NSMutableArray array];

    NSMutableArray *arr20 = [NSMutableArray array];

    NSMutableArray *arr21 = [NSMutableArray array];

    NSMutableArray *arr22 = [NSMutableArray array];

    NSMutableArray *arr23 = [NSMutableArray array];

    NSMutableArray *arr24 = [NSMutableArray array];

    NSMutableArray *arr25 = [NSMutableArray array];

    NSMutableArray *arr26 = [NSMutableArray array];

    

    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];

    [mDic setObject:arr1 forKey:@"A"];

    [mDic setObject:arr2 forKey:@"B"];

    [mDic setObject:arr3 forKey:@"C"];

    [mDic setObject:arr4 forKey:@"D"];

    [mDic setObject:arr5 forKey:@"E"];

    [mDic setObject:arr6 forKey:@"F"];

    [mDic setObject:arr7 forKey:@"G"];

    [mDic setObject:arr8 forKey:@"H"];

    [mDic setObject:arr9 forKey:@"I"];

    [mDic setObject:arr10 forKey:@"J"];

    [mDic setObject:arr11 forKey:@"K"];

    [mDic setObject:arr12 forKey:@"L"];

    [mDic setObject:arr13 forKey:@"M"];

    [mDic setObject:arr14 forKey:@"N"];

    [mDic setObject:arr15 forKey:@"O"];

    [mDic setObject:arr16 forKey:@"P"];

    [mDic setObject:arr17 forKey:@"Q"];

    [mDic setObject:arr18 forKey:@"R"];

    [mDic setObject:arr19 forKey:@"S"];

    [mDic setObject:arr20 forKey:@"T"];

    [mDic setObject:arr21 forKey:@"U"];

    [mDic setObject:arr22 forKey:@"V"];

    [mDic setObject:arr23 forKey:@"W"];

    [mDic setObject:arr24 forKey:@"X"];

    [mDic setObject:arr25 forKey:@"Y"];

    [mDic setObject:arr26 forKey:@"Z"];

    

    Contact *p1 = [[Contact alloc] initWithName:@"Arwwr" sex:@"m" age:22 adds:@"a" phone:@"188" group:@"student"];

    Contact *p2 = [[Contact alloc] initWithName:@"B" sex:@"f" age:22 adds:@"a" phone:@"188" group:@"student"];

    Contact *p3 = [[Contact alloc] initWithName:@"C" sex:@"m" age:22 adds:@"a" phone:@"188" group:@"student"];

    Contact *p4 = [[Contact alloc] initWithName:@"D" sex:@"f" age:22 adds:@"a" phone:@"188" group:@"student"];

    Contact *p5 = [[Contact alloc] initWithName:@"E" sex:@"m" age:22 adds:@"a" phone:@"177" group:@"student"];

    Contact *p6 = [[Contact alloc] initWithName:@"F" sex:@"f" age:22 adds:@"a" phone:@"177" group:@"student"];

    Contact *p7 = [[Contact alloc] initWithName:@"G" sex:@"m" age:22 adds:@"a" phone:@"177" group:@"student"];

    Contact *p8 = [[Contact alloc] initWithName:@"H" sex:@"f" age:22 adds:@"a" phone:@"177" group:@"student"];

    Contact *p9 = [[Contact alloc] initWithName:@"I" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

    Contact *p10 = [[Contact alloc] initWithName:@"J" sex:@"f" age:22 adds:@"a" phone:@"166" group:@"student"];

    Contact *p11 = [[Contact alloc] initWithName:@"K" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

    Contact *p12 = [[Contact alloc] initWithName:@"L" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

    Contact *p13 = [[Contact alloc] initWithName:@"Aoillhgf" sex:@"f" age:22 adds:@"a" phone:@"155" group:@"student"];

    Contact *p14 = [[Contact alloc] initWithName:@"Aheokjhht" sex:@"m" age:22 adds:@"a" phone:@"155" group:@"student"];

    Contact *p15 = [[Contact alloc] initWithName:@"Acvnutrh" sex:@"f" age:22 adds:@"a" phone:@"155" group:@"student"];

    Contact *p16 = [[Contact alloc] initWithName:@"Ayuid" sex:@"m" age:22 adds:@"a" phone:@"155" group:@"student"];

    Contact *p17 = [[Contact alloc] initWithName:@"Ayuidfgh" sex:@"f" age:22 adds:@"a" phone:@"144" group:@"student"];

    Contact *p18 = [[Contact alloc] initWithName:@"R" sex:@"m" age:22 adds:@"a" phone:@"144" group:@"student"];

    Contact *p19 = [[Contact alloc] initWithName:@"S" sex:@"f" age:22 adds:@"a" phone:@"144" group:@"student"];

    Contact *p20 = [[Contact alloc] initWithName:@"T" sex:@"m" age:22 adds:@"a" phone:@"144" group:@"student"];

    Contact *p21 = [[Contact alloc] initWithName:@"U" sex:@"f" age:22 adds:@"a" phone:@"133" group:@"student"];

    Contact *p22 = [[Contact alloc] initWithName:@"V" sex:@"m" age:22 adds:@"a" phone:@"133" group:@"student"];

    Contact *p23 = [[Contact alloc] initWithName:@"W" sex:@"f" age:22 adds:@"a" phone:@"133" group:@"student"];

    Contact *p24 = [[Contact alloc] initWithName:@"X" sex:@"m" age:22 adds:@"a" phone:@"133" group:@"student"];

    Contact *p25 = [[Contact alloc] initWithName:@"Y" sex:@"f" age:22 adds:@"a" phone:@"122" group:@"student"];

    Contact *p26 = [[Contact alloc] initWithName:@"Z" sex:@"f" age:22 adds:@"a" phone:@"122" group:@"student"];

    Contact *p27 = [[Contact alloc] initWithName:@"ABwerwe" sex:@"m" age:22 adds:@"a" phone:@"122" group:@"student"];

    Contact *p28 = [[Contact alloc] initWithName:@"BA" sex:@"m" age:22 adds:@"a" phone:nil group:@"student"];

    

    [arr0 addObject:p1];

    [arr0 addObject:p2];

    [arr0 addObject:p3];

    [arr0 addObject:p4];

    [arr0 addObject:p5];

    [arr0 addObject:p6];

    [arr0 addObject:p7];

    [arr0 addObject:p8];

    [arr0 addObject:p9];

    [arr0 addObject:p10];

    [arr0 addObject:p11];

    [arr0 addObject:p12];

    [arr0 addObject:p13];

    [arr0 addObject:p14];

    [arr0 addObject:p15];

    [arr0 addObject:p16];

    [arr0 addObject:p17];

    [arr0 addObject:p18];

    [arr0 addObject:p19];

    [arr0 addObject:p20];

    [arr0 addObject:p21];

    [arr0 addObject:p22];

    [arr0 addObject:p23];

    [arr0 addObject:p24];

    [arr0 addObject:p25];

    [arr0 addObject:p26];

    [arr0 addObject:p27];

    [arr0 addObject:p28];

   

    

// 3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

    

    for (NSInteger i = 0; i < 28; i++) {

        Contact *p = [arr0 objectAtIndex:i];

        if (p.phone == nil || p.name == nil) {

            NSLog(@"name: %@, phone : %@  姓名或者号码为空,添加失败", p.name, p.phone);

        }else{

            [arr0 addObject:p];

            

            }

    }

    for (NSInteger i = 0; i < 28; i++) {

        Contact *p = [arr0 objectAtIndex:i];

        

        if([p.name hasPrefix:@"A"]){

            [arr1 addObject:p];

        }else if([p.name hasPrefix:@"B"]){

            [arr2 addObject:p];

        }else if([p.name hasPrefix:@"C"]){

            [arr3 addObject:p];

        }else if([p.name hasPrefix:@"D"]){

            [arr4 addObject:p];

        }else if([p.name hasPrefix:@"E"]){

            [arr5 addObject:p];

        }else if([p.name hasPrefix:@"F"]){

            [arr6 addObject:p];

        }else if([p.name hasPrefix:@"G"]){

            [arr7 addObject:p];

        }else if([p.name hasPrefix:@"H"]){

            [arr8 addObject:p];

        }else if([p.name hasPrefix:@"I"]){

            [arr9 addObject:p];

        }else if([p.name hasPrefix:@"J"]){

            [arr10 addObject:p];

        }else if([p.name hasPrefix:@"K"]){

            [arr11 addObject:p];

        }else if([p.name hasPrefix:@"L"]){

            [arr12 addObject:p];

        }else if([p.name hasPrefix:@"M"]){

            [arr13 addObject:p];

        }else if([p.name hasPrefix:@"N"]){

            [arr14 addObject:p];

        }else if([p.name hasPrefix:@"O"]){

            [arr15 addObject:p];

        }else if([p.name hasPrefix:@"P"]){

            [arr16 addObject:p];

        }else if([p.name hasPrefix:@"Q"]){

            [arr17 addObject:p];

        }else if([p.name hasPrefix:@"R"]){

            [arr18 addObject:p];

        }else if([p.name hasPrefix:@"S"]){

            [arr19 addObject:p];

        }else if([p.name hasPrefix:@"T"]){

            [arr20 addObject:p];

        }else if([p.name hasPrefix:@"U"]){

            [arr21 addObject:p];

        }else if([p.name hasPrefix:@"V"]){

            [arr22 addObject:p];

        }else if([p.name hasPrefix:@"W"]){

            [arr23 addObject:p];

        }else if([p.name hasPrefix:@"X"]){

            [arr24 addObject:p];

        }else if([p.name hasPrefix:@"Y"]){

            [arr25 addObject:p];

        }else if([p.name hasPrefix:@"Z"]){

            [arr26 addObject:p];

        }

    }

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 根据key获取对应value

        NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

    }

 

   

//    4、获取某个分组名称下所有联系人,并且按照姓名升序排列。

    NSLog(@"23453");

    NSMutableArray *cName = [mDic objectForKey:@"A"];

    

    for (NSInteger i = 0; i < cName.count - 1; i++) {

        for (NSInteger j = 0; j < cName.count - 1 - i; j++) {

            NSComparisonResult result = [[[cName objectAtIndex:j] name] compare:[[cName objectAtIndex:j + 1] name] ];

            if (result == NSOrderedDescending) {

// 按照 a b c 排序

                [cName exchangeObjectAtIndex:j withObjectAtIndex:j + 1];

            }

 

        }

        //NSLog(@"%@", [cName objectAtIndex:i]);

       // NSLog(@"%@",[[cName objectAtIndex:i] name]);

    }

    for (NSInteger i = 0; i < cName.count ; i++) {

        NSLog(@"%@", [cName objectAtIndex:i]);

        NSLog(@"%@",[[cName objectAtIndex:i] name]);

    }

    

//    5、从通讯录中根据电话号码搜索联系人。

    

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 根据key获取对应value

        //NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

        NSArray *t = [mDic objectForKey:key];

            for (NSInteger i = 0; i < t.count; i++){

                Contact *person = [t objectAtIndex:i];

                if ([[person phone] isEqualToString:@"188"] ) {

                    NSLog(@"name %@,phone %@", person.name, person.phone);

                }

            }

    }

//    for (NSString *p in mDic) {

//        NSString *key = [mDic objectForKey:p];

//        NSArray *t = [mDic objectForKey:@"A"];

//        // NSLog(@"%@ %@", [[t objectAtIndex:2] name], [[t objectAtIndex:2] phone]);

//        for (NSInteger i = 0; i < t.count; i++){

//            Contact *person = [t objectAtIndex:i];

//            if ([[person phone] isEqualToString:@"188"] ) {

//                NSLog(@"%@, %@", person.name, person.phone);

//            }

//        }

//        

//    }

    

//    6、获取所有女性的联系人,并且按照年龄的降序排列。

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 根据key获取对应value

        //NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

        NSMutableArray *t = [mDic objectForKey:key];

        for (NSInteger i = 0; i < t.count; i++){

            Contact *person = [t objectAtIndex:i];

            if ([[person sex] isEqualToString:@"f"] ) {

                NSLog(@"name %@,sex %@", person.name, person.sex);

            }

        }

    }

 

//    7、根据姓名删除某个联系⼈。

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 根据key获取对应value

        //NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

        NSMutableArray *t = [mDic objectForKey:key];

        for (NSInteger i = 0; i < t.count; i++){

            Contact *person = [t objectAtIndex:i];

            if ([[person name] isEqualToString:@"T"] ) {

                NSLog(@"name %@,sex %@", person.name, person.sex);

                [t removeObjectAtIndex:i];

            }

        }

    }

 //

    BOOL r = YES;

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 根据key获取对应value

        //NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

        NSMutableArray *t = [mDic objectForKey:key];

    if (r) {

            

        for (NSInteger i = 0; i < t.count; i++){

            Contact *person = [t objectAtIndex:i];

            if ([[person name] isEqualToString:@"T"] ) {

               

                NSLog(@"name %@,sex %@", person.name, person.sex);

            }else {

                NSLog(@"没有 T 人 ");

                r = NO;

            }

            

        }

    }

    }

//    8、删除某个分组的全部联系人。

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        NSString *key = [mDic.allKeys objectAtIndex:i];

        // 删除

        if ([key isEqualToString:@"B"]) {

        [mDic removeObjectForKey:key];

        }

        NSLog(@"88888%@", mDic);

    }

// 打印

    for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

        // 获取每一个Key

        //NSString *key = [mDic.allKeys objectAtIndex:i];

                NSLog(@"88888%@", mDic);

    }

 

    return 0;

}

 

posted @ 2015-02-06 10:40  horacker  阅读(1388)  评论(0编辑  收藏  举报