首页 > 代码库 > 初级通讯录

初级通讯录

实现简单通讯录操作。

?1、定义联系人类AddressPerson。实例变量:姓名、性别、电话号码、住址、分组名称。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息

?2、在main.m中定义可变数组,管理所有联系人。可以添加新联系人对象,如果姓名或电话号码为空,打印添加失败。?

3、获取某个分组下的所有联系人。?

4、根据电话号码搜索联系人。

?5、获取所有女性联系人?

6、根据姓名删除联系人?

7、删除某个分组全部联系人 ?

8、展示通讯录中所有联系人

?9、选做:定义AddressBook类,封装上述功能。

AddressPerson.h

#import <Foundation/Foundation.h>// 1、定义联系人类AddressPerson。实例变量:姓名、性别、电话号码、住址、分组名称。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息@interface AddressPerson : NSObject{    NSString *_name; //姓名    NSString *_gender; //性别    NSString *_phoneNumber; //电话号码    NSString *_address; //地址    NSString *_groupName; //分组名称}//setter and getter- (void)setName:(NSString *)name;- (NSString *)name;- (void)setGender:(NSString *)gender;- (NSString *)gender;- (void)phoneNumber:(NSString *)phoneNumber;- (NSString *)phoneNumber;- (void)setAddress:(NSString *)address;- (NSString *)address;- (void)setGroupName:(NSString *)groupName;- (NSString *)groupName;// init method- (id)initWithName:(NSString *)name gender:(NSString *)gender phoneNumber:(NSString *)phoneNumber address:(NSString *)address groupName:(NSString *)groupName;//sayHi- (void)sayHi;@end

AddressPerson.m

#import "AddressPerson.h"@implementation AddressPerson//setter and getter- (void)setName:(NSString *)name{    _name = name;}- (NSString *)name{    return _name;}- (void)setGender:(NSString *)gender{    _gender = gender;}- (NSString *)gender{    return _gender;}- (void)phoneNumber:(NSString *)phoneNumber{    _phoneNumber = phoneNumber;}- (NSString *)phoneNumber{    return _phoneNumber;}- (void)setAddress:(NSString *)address{    _address = address;}- (NSString *)address{    return _address;}- (void)setGroupName:(NSString *)groupName{    _groupName = groupName;}- (NSString *)groupName{    return _groupName;}// init method- (id)initWithName:(NSString *)name gender:(NSString *)gender phoneNumber:(NSString *)phoneNumber address:(NSString *)address groupName:(NSString *)groupName{    self = [super init];    if (self) {        _name = name;        _gender = gender;        _phoneNumber = phoneNumber;        _address = address;        _groupName = groupName;    }    return self;}//sayHi- (void)sayHi{    NSLog(@"name:%@, gender:%@m, phoneNumber:%@, address:%@, groupName:%@", _name, _gender, _phoneNumber, _address, _groupName);}@end

AddressBook.h

#import <Foundation/Foundation.h>@class AddressPerson;@interface AddressBook : NSObject{    //实例变量可变数组,用来存储所有的联系人    NSMutableArray *_allPerson;}//1.获取所有联系人。- (NSMutableArray *)allPerson;//2.可以添加新联系人对象- (void)addAddressPerson:(AddressPerson *)person;//3、获取某个分组下的所有联系人。- (NSMutableArray *)getAllAddressPersonByGroupName:(NSString *)groupName;//4、根据电话号码搜索联系人。- (AddressPerson *)getAddressPersonByPhoneNumber:(NSString *)phoneNumber;//5、获取所有女性联系人- (NSMutableArray *)getAllGirlAddressPerson;//6、根据姓名删除联系人- (void)deleteAddressPersonByName:(NSString *)name;//7、删除某个分组全部联系人- (void)deleteAddressPersonByGroupName:(NSString *)groupName;//8、展示通讯录中所有联系人- (void)displayAllAddressPersonInfo;@end

AddressBook.m

#import "AddressBook.h"#import "AddressPerson.h"@implementation AddressBook//重写从父类继承过来的init初始化方法,在初始化时为数组添加多个联系人.- (id)init{    self = [super init];    if (self) {        AddressPerson *chaoQun = [[AddressPerson alloc] initWithName:@"常超群" gender:@"" phoneNumber:@"383838" address:@"中国" groupName:@"C"];        AddressPerson *qingCheng = [[AddressPerson alloc] initWithName:@"周青城" gender:@"" phoneNumber:@"1111111" address:@"大陆" groupName:@"Z"];        AddressPerson *yanYan = [[AddressPerson alloc] initWithName:@"王艳艳" gender:@"" phoneNumber:@"333333" address:@"河南" groupName:@"W"];        AddressPerson *zhangBo = [[AddressPerson alloc] initWithName:@"张博" gender:@"" phoneNumber:@"4444444" address:@"日本" groupName:@"Z"];        AddressPerson *duBo = [[AddressPerson alloc] initWithName:@"杜博" gender:@"" phoneNumber:@"222222" address:@"美国" groupName:@"D"];        AddressPerson *yuFeng = [[AddressPerson alloc] initWithName:@"赵玉凤" gender:@"" phoneNumber:@"8888888" address:@"中国" groupName:@"Z"];        //2.将联系人对象存储在可变数组中        _allPerson = [[NSMutableArray alloc] initWithObjects:chaoQun, qingCheng, yanYan, zhangBo, duBo, yuFeng, nil];    }    return self;}//1.获取所有联系人。- (NSMutableArray *)allPerson{    return _allPerson;}//2.可以添加新联系人对象- (void)addAddressPerson:(AddressPerson *)person{    //获取姓名    NSString *name = [person name];    //获取电话号码    NSString *phoneNumber = [person phoneNumber];    //可以添加新联系人对象,如果姓名或电话号码为空,打印添加失败。    if (!name || !phoneNumber) {        NSLog(@"联系人添加失败");    } else {        [_allPerson addObject:person]; //王力添加到数组中        NSLog(@"联系人添加成功");    }}//3、获取某个分组下的所有联系人。- (NSMutableArray *)getAllAddressPersonByGroupName:(NSString *)groupName{    //查找整个数组,找到分组为Z的联系人对象,然后添加到另外一个数组中.    //创建数组存储获取到的联系人,因为分组为Z的联系人可能不止一个.    NSMutableArray *groupArr = [NSMutableArray arrayWithCapacity:1];    //对数组allPerson进行快速遍历,查找分组为Z的联系人    for (AddressPerson *peron in _allPerson) {        //获取分组名        NSString *newGroupName = [peron groupName];        if ([newGroupName isEqualToString:groupName]) {            [groupArr addObject:peron];        }    }    return groupArr;}//4、根据电话号码搜索联系人。- (AddressPerson *)getAddressPersonByPhoneNumber:(NSString *)phoneNumber{    for (AddressPerson *person in _allPerson) {        NSString *newPhoneNumber = [person phoneNumber];        if ([newPhoneNumber isEqualToString:phoneNumber]) {            return person;        }    }    return nil;  //如果不存在返回空}//5、获取所有女性联系人- (NSMutableArray *)getAllGirlAddressPerson{    //查找整个数组(所有联系人),找到性别为女的联系人对象,然后添加到另外一个数组中.    //创建数组存储获取到的联系人,因为性别为女的联系人可能不止一个.所以用数组存储    NSMutableArray *grilArr = [[NSMutableArray alloc] initWithCapacity:1];    for (AddressPerson *person in _allPerson) {        NSString *gender = [person gender]; //获取到性别        if ([gender isEqualToString:@""]) {            [grilArr addObject:person];        }    }    return grilArr;}//6、根据姓名删除联系人- (void)deleteAddressPersonByName:(NSString *)name{    for (int i = 0; i < [_allPerson count];) {        AddressPerson *person = _allPerson[i];        NSString *newName = [person name];        if ([newName isEqualToString:name]) {            [_allPerson removeObjectAtIndex:i];        } else {            i++;        }    }}//7、删除某个分组全部联系人- (void)deleteAddressPersonByGroupName:(NSString *)groupName{    for (int i = 0; i < [_allPerson count];) {        AddressPerson *person = _allPerson[i];        NSString *newGroupName = [person groupName];        if ([newGroupName isEqualToString:groupName]) {            [_allPerson removeObjectAtIndex:i];        } else {            i++;        }    }}//8、展示通讯录中所有联系人- (void)displayAllAddressPersonInfo{    for (AddressPerson *person in _allPerson) {        [person sayHi];    }}@end

main.m

#import <Foundation/Foundation.h>#import "AddressPerson.h"#import "AddressBook.h"int main(int argc, const char * argv[]){    @autoreleasepool {        //1.创建一个通讯录对象        AddressBook *addBook = [[AddressBook alloc] init];        //2.获取所有联系人        NSMutableArray *allPerson = [addBook allPerson];        NSLog(@"%@", allPerson);        //输出所有联系人信息        [addBook displayAllAddressPersonInfo];        //3.添加联系人        AddressPerson *wangLi = [[AddressPerson alloc] initWithName:@"王力" gender:@"" phoneNumber:@"0000000" address:@"日本" groupName:@"W"];        [addBook addAddressPerson:wangLi];        NSLog(@"添加联系人之后的所有联系人");        //输出所有联系人信息        [addBook displayAllAddressPersonInfo];        //4.获取某个分组下的所有联系人. (获取Z分组下的所有联系人).        NSMutableArray *groupArr = [addBook getAllAddressPersonByGroupName:@"Z"];        NSLog(@"%@",groupArr);        //5.根据电话号码搜索联系人. 搜索电话号码为0000000的联系人        AddressPerson *person = [addBook getAddressPersonByPhoneNumber:@"0000000"];        NSLog(@"%@",person);        //6、获取所有女性联系人.        NSMutableArray *grilArr = [addBook getAllGirlAddressPerson];        NSLog(@"%@", grilArr);        //7、根据姓名删除联系人 查找姓名为王力的学生,然后删除掉        [addBook deleteAddressPersonByName:@"王力"];        //8、删除某个分组全部联系人, 删除分组为Z的联系人        [addBook deleteAddressPersonByGroupName:@"Z"];        //9、展示通讯录中所有联系人        NSLog(@"输出所有联系人信息");        [addBook displayAllAddressPersonInfo];    }    return 0;}

 

初级通讯录