YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

OC-通讯录-分组-删除-判断 重新setter getter

/*

 完成以下需求
 需求: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



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;// 年龄
// 初始化

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;
// 初始化

// settet getter

// 便利构造器

// 描述方法




当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »