Objective-c实现通讯录(17-08-01)

 完成以下需求
 需求:1、定义联系人类Contact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息。
 2、在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英文字母。
 3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。
 4、删除某个分组的全部联系人。
//
//  main.m
//  通讯录
//
//  Created by lanou3g on 17/8/1.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import 
#import "Contact.h"

int main(int argc, const char * argv[]) {
    
    NSMutableDictionary *contactDic = [NSMutableDictionary dictionary];
    for (char c = 'A'; c <= 'Z'; c++) {
        NSString *key = [NSString stringWithFormat:@"%c",c];
        NSMutableArray *value = [NSMutableArray array];
        [contactDic setObject:value forKey:key];
    }
    Contact *contact = [[Contact alloc] initWithName:@"lee" phoneNumber:@"123456" groupName:@"L"];
    if (contact.name != nil && contact.name.length != 0 && contact.phoneNumber != nil && contact.phoneNumber.length != 0) {
        //在字典中查找相应的分组,放到数组中
        NSMutableArray *contactArray = [contactDic objectForKey:contact.groupName];
        [contactArray addObject:contact];
    }else {
        NSLog(@"姓名和电话不能为空");
    }
    //遍历字典取出来信息
    NSArray *keyArray = contactDic.allKeys;
    for (int i = 0; i < keyArray.count; i++) {
        NSString *key = [keyArray objectAtIndex:i];
        NSMutableArray *contactArray = [contactDic objectForKey:key];
        for (int j = 0; j < contactArray.count; j++) {
            Contact *contact = [contactArray objectAtIndex:j];
            [contact showInfo];
        }
    }
    
    return 0;
}
//
//  Contact.h
//  通讯录
//
//  Created by lanou3g on 17/8/1.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import 

@interface Contact : NSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *gender;
@property (nonatomic, retain) NSString *phoneNumber;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *groupName;
@property (nonatomic, assign) NSInteger age;

- (instancetype)initWithName:(NSString *)name phoneNumber:(NSString *)phoneNumber groupName:(NSString *)groupName;
- (void)showInfo;

@end
//
//  Contact.m
//  通讯录
//
//  Created by lanou3g on 17/8/1.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "Contact.h"

@implementation Contact

- (instancetype)initWithName:(NSString *)name phoneNumber:(NSString *)phoneNumber groupName:(NSString *)groupName {
    self = [super init];
    if (self) {
        _name = name;
        _phoneNumber = phoneNumber;
        _groupName = groupName;
    }
    return self;
}
- (void)showInfo {
    NSLog(@"name=%@,age=%ld,phoneNumber=%@,groupName=%@,address=%@,gender=%@",_name,_age,_phoneNumber,_groupName,_address,_gender);
}

@end

你可能感兴趣的:(Objective-c实现通讯录(17-08-01))