HHH航_IOS开发学习之路

还在进步中的小菜鸟,如若写的不当,还望各路大神多多指正

OC简易通讯录

// AddressBook.m

import “AddressBook.h”

@implementation AddressBook

//重写初始化方法
//- (instancetype)init
//{
// self = [super init];
// if (self) {
// //初始化对象的同时 初始化数组
// _contactArray = [NSMutableArray array];
// }
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
_contactArray = [NSMutableArray array];
}
return self;
}
//添加新联系人
//- (void)addContact:(Contact *)contact
//{
//
// if ([[contact name]length] == 0 || [[contact telephone]length ] == 0) {
// NSLog(@”打印失败”);
// }else
// {
// [_contactArray addObject:contact];
// }
//}
- (void)addContact:(Contact *)contact
{
if ([[contact name]length] == 0 || [[contact telephone]length] == 0) {
NSLog(@”打印失败”);
}else
{
[_contactArray addObject:contact];
}
}
//获取某个分组下的所有联系⼈
- (void)getGroupAllContactByGroupName:(NSString *)groupName
{
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人
Contact *contact = _contactArray[i];
//判断是否与分组名相同 相同 打印
if ([[contact groupName] isEqualToString:groupName]) {
NSLog(@”%@”,contact);
}

}

}

//通过电话号码查找联系人
- (void)findContactByTelephone:(NSString *)telephone
{
//遍历整个数组
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人
Contact *contact = _contactArray[i];
//判断电话号码是否相等 相等 输出
if ([[contact telephone] isEqualToString:telephone]) {
NSLog(@”%@”,contact);
}
}

}

//获取所有⼥性联系⼈;
- (void)findAllWomanContact:(NSString *)sex
{
//遍历整个数组
for (int i = 0; i < [_contactArray count]; i++) {
//取出每个联系人

    Contact *contact = _contactArray[i];
    //判断性别是否相等  相等 输出
    if ([[contact sex] isEqualToString:sex]) {
        NSLog(@"%@",contact);
    }
}

}

//根据姓名删除联系⼈
- (void)deleteContactByName:(NSString *)name
{
for (int i = 0; i < [_contactArray count]; i++) {

    //判断姓名是否相等  相等 删除
    if ([[_contactArray[i] name] isEqualToString:name]) {
        [_contactArray  removeObject:_contactArray[i]];
        i--;
    }else {
    NSLog(@"%@",_contactArray[i]);
    }
}

}
//删除某个分组全部联系⼈
- (void)deleteGroupAllContactByGroupName:(NSString *)groupName
{
for (int i = 0; i < [_contactArray count]; i++) {

    //判断分组名称是否相等  相等 删除
    if ([[_contactArray[i] groupName] isEqualToString: groupName]) {
        [_contactArray  removeObject:_contactArray[i]];
        i--;
    }else
    {
        NSLog(@"%@",_contactArray[i]);
    }

}

}
//展⽰通讯录中所有联系⼈
- (void)printfAllContact
{
for (int i = 0; i < [_contactArray count]; i++) {
NSLog(@”%@”,_contactArray[i]);
}
}

// main .m
Contact *c1 = [[Contact alloc]initWithName:@”尼鹏” sex:@”不祥” telephone:@”123456789” address:@”妓院” groupName:@”D”];
Contact *c2 = [[Contact alloc]initWithName:@”陆桃桃” sex:@”男” telephone:@”12345” address:@”学院” groupName:@”C”];
Contact *c3 = [[Contact alloc]initWithName:@”大军” sex:@”女” telephone:@”1234569” address:@”学习院” groupName:@”B”];
Contact *c4 = [[Contact alloc]initWithName:@”徐阳” sex:@”女” telephone:@”123459” address:@”试试” groupName:@”C”];
Contact *c5 = [[Contact alloc]initWithName:@”流汗” sex:@”男” telephone:@”13456789” address:@”55院” groupName:@”D”];
AddressBook *address = [[AddressBook alloc]init];
[address addContact:c1];
[address addContact:c2];
[address addContact:c3];
[address addContact:c4];
[address addContact:c5];
[address deleteGroupAllContactByGroupName:@”B”];
NSLog(@”\n”);
[address printfAllContact];
NSLog(@”\n”);
[address getGroupAllContactByGroupName:@”同学”];
NSLog(@”\n”);
[address findAllWomanContact:@”女”];
NSLog(@”\n”);
[address findContactByTelephone:@”123456789”];
NSLog(@”\n”);
[address deleteContactByName:@”大军”];
NSLog(@”\n”);
[address deleteContactByName:@”尼鹏”];
NSLog(@”\n”);

[address deleteContactByName:@"徐阳"];
NSLog(@"\n");
[address printfAllContact];

NSLog(@”\n”);
[address printfAllContact];
NSLog(@”\n”)

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OnthewayH/article/details/49902947
个人分类: OC
想对作者说点什么? 我来说一句

AddressBook通讯录

2015年10月20日 42KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭