中等难度的通讯录.字典 动态分组法
//
// main.m
// 练习-中等通讯录
//
// Created by dllo on 15/11/4.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Contact.h"
#import "Manager.h"
int main(int argc, const char * argv[]) {
//实现中等难度通讯录。需求:
// 1、定义联系人类Contact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息
// 2、在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英文字母。
// 3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。
Contact *str1 = [[Contact alloc]initWithName:@"yass" sex:@"sex" address:@"dsa" group:@"1" phone:@"1234566" age:22];
Contact *str2 = [[Contact alloc]initWithName:@"ybuaho" sex:@"sex" address:@"dsa" group:@"1" phone:@"ss" age:22];
Contact *str3 = [[Contact alloc]initWithName:@"ycho" sex:@"sex" address:@"dsa" group:@"1" phone:@"ss" age:22];
Manager *p = [[Manager alloc]init];
[p addcontact:str1];
[p addcontact:str2];
[p addcontact:str3];
//[p info];
// 4、获取某个分组名称下所有联系人,并且按照姓名升序排列。
// NSArray *b = [p getSortedByNameAllcontactOfGroup:@"y"];
// for (Contact *contact in b) {
// NSLog(@"%@ %@ %@ %@ %@ %ld",[contact name], [contact sex], [contact adress], [contact group], [contact phone], [contact age]);
// }
// 5、从通讯录中根据电话号码搜索联系人。
NSArray *b = [p getcontactByphone:@"ss"];
for (Contact *contact in b) {
NSLog(@"%@ %@ %@ %@ %@ %ld",[contact name], [contact sex], [contact adress], [contact group], [contact phone], [contact age]);
}
// 6、获取所有女性的联系人,并且按照年龄的降序排列。
// 7、根据姓名删除某个联系人。
// 8、删除某个分组的全部联系人。
return 0;
}
//
// Manager.h
// 练习-中等通讯录
//
// Created by dllo on 15/11/4.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Contact;
@interface Manager : NSObject
@property (nonatomic, retain) NSMutableDictionary *contactDic;
- (id)init;
//打印所有
- (void)info;
//2、在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英文字母。
- (void)addcontact:(Contact *)contact;
// 4、获取某个分组名称下所有联系人,并且按照姓名升序排列。
- (NSMutableArray *)getSortedByNameAllcontactOfGroup:(NSString *)key;
// 5、从通讯录中根据电话号码搜索联系人。
- (NSMutableArray *)getcontactByphone:(NSString *)phone;
@end
//
// Manager.m
// 练习-中等通讯录
//
// Created by dllo on 15/11/4.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "Manager.h"
#import "Contact.h"
@implementation Manager
- (id)init
{
self = [super init];
if (self) {
self.contactDic = [NSMutableDictionary dictionary
]; }
return self;
}
//打印所有
- (void)info;
{
for (NSString *key in self.contactDic) {
for (Contact *c in [self.contactDic valueForKey:key]) {
NSLog(@"%@ %@ %@ %@ %@ %ld", [c name], [c sex], [c adress], [c phone], [c group], [c age]);
}
}
}
//2、在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英文字母。
//3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。
- (void)addcontact:(Contact *)contact
{
if (0 == contact.name.length || 0 == contact.phone.length) {
NSLog(@"添加失败");
return;
}
NSString *key = [contact.name substringToIndex:1];
NSMutableArray *arr = nil;
if (nil == [self.contactDic valueForKey:key]) {
arr = [NSMutableArray array];
[self.contactDic setValue:arr forKey:key];
} else {
arr = [self.contactDic valueForKey:key];
}
[arr addObject:contact];
}
// 4、获取某个分组名称下所有联系人,并且按照姓名升序排列。
- (NSMutableArray *)getSortedByNameAllcontactOfGroup:(NSString *)key
{
if (0 == key.length) {
return nil;
}
NSMutableArray *arr = [self.contactDic valueForKey:key];
[arr sortedArrayUsingSelector:@selector(compareByName:)];
return arr;
}
// 5、从通讯录中根据电话号码搜索联系人。
- ( NSMutableArray *)getcontactByphone:(NSString *)phone
{
if (0 == phone.length) {
return nil;
}
NSMutableArray *Arr = [NSMutableArray array];
for (NSString *key in self.contactDic) {
for (Contact *b in [self.contactDic valueForKey:key]) {
if ([b.phone isEqualToString:phone]) {
[Arr addObject:b];
}
}
}
return Arr;
}
@end
//
// Contact.m
// 练习-中等通讯录
//
// Created by dllo on 15/11/4.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "Contact.h"
@implementation Contact
- (id)initWithName:(NSString *)name sex:(NSString *)sex address:(NSString *)address group:(NSString *)group phone:(NSString *)phone age:(NSInteger)age{
self = [super self];
if (self) {
[self setName:name];
[self setSex:sex];
[self setAdress:address];
[self setAge:age];
[self setGroup:group];
[self setPhone:phone];
}
return self;
}
- (NSComparisonResult)compareByName:(Contact *)contact
{
return [contact.name compare:self.name];
}
@end
//
// Contact.h
// 练习-中等通讯录
//
// Created by dllo on 15/11/4.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Manager;
@interface Contact : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *adress;
@property (nonatomic, copy) NSString *group;
@property (nonatomic, copy) NSString *phone;
@property (nonatomic, assign)NSInteger age;
- (id)initWithName:(NSString *)name sex:(NSString *)sex address:(NSString *)address group:(NSString *)group phone:(NSString *)phone age:(NSInteger)age;
- (NSComparisonResult)compareByName:(Contact *)contact;
//(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
@end