#import <Foundation/Foundation.h>
@interface AdressContact : NSObject
{
NSString *_name;
NSString *_sex;
NSInteger _number;
NSString *_address;
NSString *_groupName;
NSInteger _age;
}
@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSString *sex;
@property NSInteger number;
@property(nonatomic, retain) NSString *address;
@property(nonatomic, retain) NSString *groupName;
@property NSInteger age;
//初始化姓名和电话号码
- (instancetype)initWihtName:(NSString *)name number:(NSInteger)number;
//便利构造器
+ (AdressContact *)addressContactWithName:(NSString *)name number:(NSInteger)number;
//显示信息
- (void)printAddressContact;
- (NSString *)description;
@end
#import "AdressContact.h"
@implementation AdressContact
@synthesize age=_age,name=_name,sex=_sex,number=_number,address=_address,groupName=_groupName;
//初始化姓名和电话号码
- (instancetype)initWihtName:(NSString *)name number:(NSInteger)number
{
if (self=[super init]) {
_name=name;
_number=number;
}
return self;
}
//便利构造器
+ (AdressContact *)addressContactWithName:(NSString *)name number:(NSInteger)number
{
return [[AdressContact alloc]initWihtName:name number:number];
// AdressContact *adC = [[AdressContact alloc] initWithName:name number:number];
// return [adC autorelease];
}
- (void)printAddressContact
{
NSLog(@"姓名:%@ 性别:%@ 电话:%ld 地址:%@ 分组:%@ 年龄:%ld",_name,_sex,_number,_address,_groupName,_age);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %ld",_name,_number];
}
@end
#import <Foundation/Foundation.h>
#import "AdressContact.h"
@interface AdressBook : NSObject
{
NSMutableDictionary *_mdict;//存放关键字的字典
}
//添加对象
- (void)addAdressContactObject:(AdressContact *)addressContact;
//获得某个分组名称下所有联系人,并且按照姓名升序排列
- (NSArray *)getAdressContactWithGroupName:(NSString *)groupName;
//从通讯录中根据电话号码搜索联系人
- (AdressContact *)getAdressContactWithNumber:(NSInteger)number;
//获取所有的女性的联系人,并且按照年龄的升序排列
- (NSArray *)getAdressContactWithSex:(NSString *)sex;
//根据姓名删除某个联系人
- (void)removeAdressContactWithName:(NSString *)name;
//删除某个分组的全部联系人
- (void)removeAdressContactWithGroupName:(NSString *)groupName;
@end
#import "AdressBook.h"
@implementation AdressBook
{
BOOL isFind;
}
- (instancetype)init
{
if (self=[super init])
{
_mdict=[[NSMutableDictionary alloc]init];
}
return self;
}
//添加对象
- (void)addAdressContactObject:(AdressContact *)addressContact
{
if ([addressContact name] == nil || [[addressContact name]isEqualToString:@""] || [addressContact number] == 0) {
NSLog(@"添加失败");
} else {
NSString *group1 = [addressContact.name substringToIndex:1];
NSMutableArray *group = [_mdict objectForKey:group1];
if (group == nil) {
group = [[NSMutableArray alloc]init];
[group addObject:addressContact];
[_mdict setObject:group forKey:group1];
} else {
[group addObject:addressContact];
}
}
}
//获得某个分组名称下所有联系人,并且按照姓名升序排列
- (NSArray *)getAdressContactWithGroupName:(NSString *)groupName;
{
NSMutableArray *array=[NSMutableArray array];
array=[_mdict objectForKey:groupName];
return array;
}
//从通讯录中根据电话号码搜索联系人
- (AdressContact *)getAdressContactWithNumber:(NSInteger)number
{
AdressContact *addressContat1=[[AdressContact alloc]init];
for (NSString *p in _mdict) {
NSMutableArray *array=[[NSMutableArray alloc]init];
[array addObjectsFromArray:[_mdict objectForKey:p]];
for (id q in array) {
if ([q number]==number) {
addressContat1=q;
}
}
}
return addressContat1;
}
//获取所有的女性的联系人,并且按照年龄的升序排列
- (NSArray *)getAdressContactWithSex:(NSString *)sex
{
NSMutableArray *array1=[NSMutableArray array];
for (NSString *p in _mdict) {
NSMutableArray *array=[[NSMutableArray alloc]init];
[array addObjectsFromArray:[_mdict objectForKey:p]];
for (id q in array) {
if ([[q sex]isEqualToString:@"女"]) {
[array1 addObject:q];
}
}
}
return array1;
}
//根据姓名删除某个联系人
- (void)removeAdressContactWithName:(NSString *)name
{
NSMutableArray *array=[NSMutableArray array];
AdressContact *p2=[[AdressContact alloc]init];
NSString *p3 = nil;
for (NSString *p in _mdict) {
for (id q in [_mdict objectForKey:p]) {
if ([[q name]isEqualToString:name]) {
p2=q;
p3=p;
[array addObject:q];
}
}
}
[[_mdict objectForKey:p3] removeObjectsInArray:array];
}
//删除某个分组的全部联系人
- (void)removeAdressContactWithGroupName:(NSString *)groupName
{
NSMutableArray *array=[NSMutableArray array];
for (id p in _mdict) {
if ([p isEqualToString:groupName]) {
[array addObject:p];
}
}
[_mdict removeObjectsForKeys:array];
}
@end
#import <Foundation/Foundation.h>
#import "AdressContact.h"
#import "AdressBook.h"
int main(int argc, const char * argv[]) {
AdressContact *p1=[AdressContact addressContactWithName:@"Asan" number:10100];
p1.sex=@"男";
p1.address=@"北京";
p1.groupName=@"A";
p1.age=26;
AdressContact *p2=[AdressContact addressContactWithName:@"Bsi" number:10109];
p2.sex=@"女";
p2.address=@"深圳";
p2.groupName=@"B";
p2.age=20;
AdressContact *p3=[AdressContact addressContactWithName:@"Awu" number:10102];
p3.age=30;
p3.sex=@"男";
p3.address=@"河南";
p3.groupName=@"A";
AdressContact *p4=[AdressContact addressContactWithName:@"Cliu" number:10103];
p4.age=10;
p4.sex=@"女";
p4.address=@"上海";
p4.groupName=@"C";
AdressContact *p5=[AdressContact addressContactWithName:@"Aqi" number:10104];
p5.age=9;
p5.sex=@"男";
p5.address=@"南京";
p5.groupName=@"A";
AdressContact *p6=[AdressContact addressContactWithName:@"Cba" number:10105];
p6.age=18;
p6.sex=@"女";
p6.address=@"苏州";
p6.groupName=@"C";
AdressContact *p8=[AdressContact addressContactWithName:@"Clan" number:10106];
p8.age=29;
p8.sex=@"男";
p8.address=@"南京";
p8.groupName=@"C";
AdressContact *p9=[AdressContact addressContactWithName:@"Didu" number:10107];
p9.age=20;
p9.sex=@"女";
p9.address=@"广州";
p9.groupName=@"D";
//创建地址薄对象
AdressBook *adressBook=[[AdressBook alloc]init];
//添加对象
[adressBook addAdressContactObject:p1];
[adressBook addAdressContactObject:p2];
[adressBook addAdressContactObject:p3];
[adressBook addAdressContactObject:p4];
[adressBook addAdressContactObject:p5];
[adressBook addAdressContactObject:p6];
[adressBook addAdressContactObject:p8];
[adressBook addAdressContactObject:p9];
//获得某个分组名称下所有的联系人
NSArray *array=[[NSArray alloc]init];
array=[adressBook getAdressContactWithGroupName:@"A"];
NSLog(@"%@",array);
//从通讯录中根据电话号码搜索联系人
[adressBook getAdressContactWithNumber:10100];
//获得所有女性的联系人
[adressBook getAdressContactWithSex:@"女"];
//根据姓名删除某个联系人
[adressBook removeAdressContactWithName:@"Aqi"];
//删除某个分组的全部联系人
[adressBook removeAdressContactWithGroupName:@"C"];
return 0;
}