#import <Foundation/Foundation.h>
#import "AddressPerson.h"
#import "AddressBook.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//定义5个对象
AddressPerson *p1 = [[AddressPerson alloc]init];
NSDictionary *dic = @{@"name":@"fu",@"number":@"12345",@"email":@"254@465",@"address":@"nanjing"};
[p1 setValuesForKeysWithDictionary:dic];
AddressPerson *p2 = [[AddressPerson alloc]init];
NSDictionary *dic2 = @{@"name":@"yang",@"number":@"12345",@"email":@"@",@"address":@"nanjing"};
[p2 setValuesForKeysWithDictionary:dic2];
AddressPerson *p3 = [[AddressPerson alloc]init];
NSDictionary *dic3 = @{@"name":@"li",@"number":@"12345",@"email":@"8@47",@"address":@"nanjing"};
[p3 setValuesForKeysWithDictionary:dic3];
AddressPerson *p4 = [[AddressPerson alloc]init];
NSDictionary *dic4 = @{@"name":@"1mei",@"number":@"14345",@"email":@"6@45",@"address":@"nanjing"};
[p4 setValuesForKeysWithDictionary:dic4];
AddressPerson *p5 = [[AddressPerson alloc]init];
NSDictionary *dic5 = @{@"name":@"",@"number":@"636",@"email":@"674@45",@"address":@"taiyangxi"};
[p5 setValuesForKeysWithDictionary:dic5];
//添加一个book对象
AddressBook *book = [[AddressBook alloc]init];
[book addressPerson:p1];
[book addressPerson:p2];
[book addressPerson:p3];
[book addressPerson:p4];
[book addressPerson:p5]; //将对象过滤添加到book中
[book sortedPerson:p1];
[book sortedPerson:p2];
[book sortedPerson:p3];
[book sortedPerson:p4];
[book sortedPerson:p5]; //将联系人进行排序
NSLog(@"%@",book); //输出符合条件的联系人 并排序
NSArray *tempArray = [book searchWithName:@"li"];//根据姓名查找联系人
NSLog(@"%@",tempArray);
NSLog(@"%@",[book sortedAllPerson]); //输出所有联系人
@interface AddressPerson : NSObject
@property(nonatomic, assign, readwrite)NSString *name, *number,*address,*email;
@end
@implementation AddressPerson
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %@ %@ %@", _name, _number,_email,_address];
}
@end
@class AddressPerson;
@interface AddressBook : NSObject
@property(nonatomic) NSMutableArray *bookArray;
@property(nonatomic)NSMutableDictionary *addBook;
-(void)addressPerson:(AddressPerson *)addPerson;
-(void)sortedPerson:(AddressPerson *)sPerson;
-(NSArray *)searchWithName:(AddressPerson *)aName;
-(NSArray *)sortedAllPerson;
@end
#import "AddressBook.h"
#import "AddressPerson.h"
@implementation AddressBook
-(id)init{
if (self = [super init]) {
_bookArray = [[NSMutableArray alloc]initWithCapacity:0];
_addBook = [[NSMutableDictionary alloc]initWithDictionary:0];
NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"*", nil];
for (NSString *key in array) {
[_addBook setObject:[NSMutableArray arrayWithCapacity:0] forKey:key];
}
}
return self;
}
//添加联系人(姓名为空 邮箱格式不正确不添加)
-(void)addressPerson:(AddressPerson *)addPerson{
if ([addPerson.name length] == 0) {
NSLog(@"该联系人名字为空!");
return;
}
if (![addPerson.email containsString:@"@"]) {
NSLog(@"%@这个邮箱格式不正确",addPerson.name);
}else{
NSArray *subString = [addPerson.email componentsSeparatedByString:@"@"];
if ([subString[0] length] == 0 || [subString[1] length ]== 0) {
NSLog(@"%@的邮箱格式不对",addPerson.name);
return;
}
}
[_bookArray addObject:addPerson];
}
//按首字母排序
-(void)sortedPerson:(AddressPerson *)sPerson{
if ([sPerson.name length]!= 0) {
NSArray *firstLetter = [[sPerson.name substringToIndex:1]capitalizedString];
NSArray *array = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"*", nil];
if ([array containsObject:firstLetter]) {
[[_addBook objectForKey:firstLetter] addObject:sPerson];
}else{
[[_addBook objectForKey:@"*"] addObject:sPerson];
}
}
else{
return;
}
}
//根据姓名查找联系人
-(NSArray *)searchWithName:(AddressPerson *)aName{
NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:0];
for (AddressPerson *person in _bookArray) {
if ([person.name isEqualToString: aName]) {
[tempArray addObject:person];
}
}
return tempArray;
}
//联系人排序
-(NSArray *)sortedAllPerson{
return [_bookArray sortedArrayUsingComparator:^NSComparisonResult(AddressPerson *obj1, AddressPerson *obj2) {
return [obj1.name compare:obj2.name];
}];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %@", _bookArray,_addBook];
}
@end
原文:http://9864900.blog.51cto.com/9854900/1620168