您的位置:首页 > 其它

通讯录

2015-12-26 21:55 239 查看
用OC实现一个中等难度的通讯录

创建两个类,一个联系人类,一个电话簿类用来管理联系人

电话本.m文件

@implementation AddressBook
-(instancetype)init{
    self = [super self];
    if (self) {
        _groupDic = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    return self;
}

//获取名字首字母
- (NSString *)getFirstName:(Contact *)contact{
    return[[[contact name] substringToIndex:1] uppercaseString];
}

//根据首字母把联系人加入分组
- (void)addContactByName:(Contact *)contact{
    if ([[contact name] length] == 0 || [contact phoneNum] == 0) {
        NSLog(@"添加失败");
    }else{
        NSMutableArray *arr = [_groupDic objectForKey:[self getFirstName:contact]];
        if (arr == nil) {[
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            [arr addObject:contact];
            [_groupDic setValue:arr forKey:[self getFirstName:contact]];
        }else{
            [arr addObject:contact];
        }
    }
}

    //获取某个分组下得所有联系人,并且按照姓名升序排序(三种方法实现)

//(1),
//    - (void)getContactOfGroupAndSorted:(NSString *)groupName{
//        NSMutableArray *arr1 = [_groupDic objectForKey:groupName];
//       // NSMutableArray *newarr = [_groupDic objectForKey:groupName];
//        for (int i = 0; i < [arr1 count] - 1; i++) {
//            for (int j = 0; j < [arr1 count] - i - 1; j++) {
//                if ([[arr1[j] name] compare:[arr1[j + 1] name]] > 0) {
//                    [arr1 exchangeObjectAtIndex:j withObjectAtIndex: j + 1];
//                }
//            }
//        }
//    }

//(2)
//- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
//    NSMutableArray *arr2 = [_groupDic objectForKey:groupName];
//    NSComparator myBlock = ^(id obj1, id obj2){
//        return [[obj1 name] compare:[obj2 name]];
//    };
//    [arr2 sortUsingComparator:myBlock];
//    NSLog(@"%@", arr2);
//  }

//(3)
- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
    NSMutableArray *arr3 = _groupDic[groupName];
    [arr3 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [[obj1 name] compare:[obj2 name]];
    }];
    NSLog(@"%@", arr3);
}

//从通讯录中根据电话号码搜索联系人
- (void)getConctByPhoneNum:(NSUInteger)phoneNum{
    NSArray *arr4 = [_groupDic allValues];
    for (NSMutableArray *arr in arr4) {
        for (Contact *c in arr) {
            if ([c phoneNum] == phoneNum) {
                NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
            }
        }
    }
}

//获取所以女性联系人,并且按照年龄的降序排列
- (void)getWContactAndSorted:(NSString *)sex{

    //获取字典中所有的联系人数组
    NSArray *arr5 = [_groupDic allValues];

    //用来接受女性联系人
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:1];

    //遍历联系人数组
    for (NSArray *a in arr5) {

        //遍历联系人数组中的联系人
        for (Contact *c in a) {
            if ([[c sex] isEqualToString:@"w"]) {
                NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
                 [mArr addObject:c];
            }
        }
    }

    //按年龄降序排列
    [mArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 age] < [obj2 age]) {
            return NSOrderedDescending;
        }else{
            return NSOrderedSame;
        }
    }];
}

//根据姓名删除某个联系人
- (void)removeContactByName:(NSString *)name{

    //获取名字首字母并转化成大写
    NSString *str = [[name substringFromIndex:1] uppercaseString];
    for (int i = 0; i < [[_groupDic valueForKey:str] count]; i++) {
        if ([[[[_groupDic valueForKey:str] objectAtIndex:i] name] isEqualToString:name]) {
            [[_groupDic objectForKey:str] removeObjectAtIndex:i];

            //如果对应分组里的联系人为空,将对应的key值也删除
            if ([_groupDic objectForKey:str] == nil) {
                [_groupDic removeObjectForKey:str];
            }
        }
    }
}

//删除某个分组的全部联系人
- (void)removeAllContactOfGroup:(NSString *)groupName{
    [_groupDic removeObjectForKey:groupName];
}

//展示通讯录中的所有联系人
- (void)printAllContactInfo{
    NSArray *cArr = [_groupDic allValues];
    for (NSMutableArray *arr in cArr) {
        for (Contact *contact in arr) {
            NSLog(@"%@", contact);
        }
    }
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Contact *con1 = [[Contact alloc] initWithName:@"Curry" sex:@"m" phoneNum:145 address:@"lanou"  age:18];
        Contact *con2 = [[Contact alloc] initWithName:@"Aobe" sex:@"w" phoneNum:123 address:@"wanf"  age:18];
        Contact *con3 = [[Contact alloc] initWithName:@"Bob" sex:@"m" phoneNum:466 address:@"henan"  age:18];
        AddressBook *AddBook = [[AddressBook alloc] init];
        [AddBook addContactByName:con1];
        [AddBook addContactByName:con2];
        [AddBook addContactByName:con3];
 //      [AddBook getWContactAndSorted:@"w"];
        //[AddBook printAllContactInfo];
        [AddBook getConctByPhoneNum:123];
        [AddBook printAllContactInfo];       
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OC 通讯录 管理 class