/*

 完成以下需求
 需求:1、定义 联系人类Contact。实例变量:姓名(拼⾳, ⾸字母大写)、性别、电话号码、住址、分组名称、年龄。 ⽅法:自定义初始化方法(姓名、电话号码)、显示联系⼈信息(重写description方法, 打印所有成员变量)。
 2、在main.m中定义字典,分组管理所有联系人。分组名 为26个大写的英文字母。
 3、可以添加联系人对象,如果姓名或电话号码为空,添加失败(NSLog错误信息)。添加联系⼈到匹配的分组。
 4、删除某个分组的全部联系⼈。
 */

main.n

#if 1
// 创建联系人字典
NSMutableDictionary *contactDic = [NSMutableDictionary dictionary];
// 循环'A'->'Z'
for (char i = 'A'; i < 'Z'; i++) {
    // key
    NSString *key = [NSString stringWithFormat:@"%c", i];
    // Value
    NSMutableArray *arr = [NSMutableArray array];
    // 添加到字典
    [contactDic setObject:arr forKey:key];
}
NSLog(@"%@", contactDic);
// 添加
Contact *c1 = [Contact contactWithName:@"YIem" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:01];
Contact *c2 = [Contact contactWithName:@"YI" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:02];
Contact *c3 = [Contact contactWithName:@"Bian" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:03];
Contact *c4 = [Contact contactWithName:@"YIemYi" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:04];
Contact *c5 = [Contact contactWithName:@"YIemYIem" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:05];
Contact *c6 = [Contact contactWithName:@"YIemem" phone:@"yiem" sex:@"yiem" address:@"yiem" groupName:@"yiem" age:06];
// 购建数组
NSArray *cArr = @[c1, c2, c3, c4, c5, c6];
// 遍历
for (Contact *c in cArr) {
    // 判断
    if (c.name.length == 0 || c.phone.length == 0) {
        NSLog(@"添加失败,姓名和电话不能为空");
    }   else {
        // 成功
        // 获取Key
        NSString *key = [c.name substringToIndex:1];
        // 获取数组
        NSMutableArray *arr = [contactDic objectForKey:key];
        // 添加
        [arr addObject:c];
    }
}
// 删除
[contactDic removeObjectForKey:@"A"];
NSLog(@"%@", contactDic);

endif

屏幕快照 2016-01-09 下午4.24.10.png
屏幕快照 2016-01-09 下午4.24.27.png
屏幕快照 2016-01-09 下午4.25.18.png

Contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject
{

NSString *_name;

}
@property (nonatomic, copy) NSString *name;// 姓名
@property (nonatomic, copy) NSString *sex;// 性别
@property (nonatomic, copy) NSString *phone;// 电话
@property (nonatomic, copy) NSString *address;// 地址
@property (nonatomic, copy) NSString *groupName;// f分组
@property (nonatomic, assign) NSInteger age;// 年龄
// 初始化

  • (instancetype)initWithName:(NSString *)name

                       phone:(NSString *)phone
                         sex:(NSString *)sex
                     address:(NSString *)address
                   groupName:(NSString *)groupName
                         age:(NSInteger)age;

    // 遍历构造器

  • (instancetype)contactWithName:(NSString *)name

                          phone:(NSString *)phone
                            sex:(NSString *)sex
                        address:(NSString *)address
                      groupName:(NSString *)groupName
                            age:(NSInteger)age;

    @end

屏幕快照 2016-01-09 下午4.25.36.png

Contact.m

@implementation Contact

// 同时重新getter和setter方法 不会自动产生成员变量 需要重新设置synthesize;
@synthesize name = _naem;
@synthesize phone = _phone;
@synthesize sex = _sex;
@synthesize address = _address;
@synthesize groupName = _groupName;
@synthesize age = _age;
// 初始化

  • (instancetype)initWithName:(NSString )name phone:(NSString )phone sex:(NSString )sex address:(NSString )address groupName:(NSString *)groupName age:(NSInteger)age
    {
    self = [super init];
    if (self) {

    self.name = name;
    self.phone = phone;
    self.sex = sex;
    self.address = address;
    self.groupName = groupName;
    self.age =age;

    }
    return self;
    }

// settet getter

  • (void)setName:(NSString *)name
    {
    _name = name;
    }
  • (NSString *)name
    {
    return _name;
    }
  • (void)setPhone:(NSString *)phone
    {
    _phone = phone;
    }
  • (NSString *)phone
    {
    return _phone;
    }
  • (void)setSex:(NSString *)sex
    {
    _sex = sex;
    }
  • (NSString *)sex
    {
    return _sex;
    }
  • (void)setAddress:(NSString *)address
    {
    _address = address;
    }
  • (NSString *)address
    {
    return _address;
    }
  • (void)setGroupName:(NSString *)groupName
    {
    _groupName = groupName;
    }
  • (NSString *)groupName
    {
    return _groupName;
    }
  • (void)setAge:(NSInteger)age
    {
    _age = age;
    }
  • (NSInteger)age
    {
    return _age;
    }

// 便利构造器

  • (instancetype)contactWithName:(NSString )name phone:(NSString )phone sex:(NSString )sex address:(NSString )address groupName:(NSString *)groupName age:(NSInteger)age
    {
    Contact *c = [[Contact alloc] initWithName:name phone:phone sex:sex address:address groupName:groupName age:age];
    return c;
    }

// 描述方法

  • (NSString *)description
    {
    return [NSString stringWithFormat:@"name: %@- phone:%@- sex:%@- address%@- groupName:%@- age:%ld-", self.name, self.phone, self.sex, self.address, self.groupName, self.age];
    }

屏幕快照 2016-01-09 下午4.25.52.png
屏幕快照 2016-01-09 下午4.26.16.png
屏幕快照 2016-01-09 下午4.26.16.png
屏幕快照 2016-01-09 下午4.26.25.png