通讯录
2015-12-26 21:55
239 查看
用OC实现一个中等难度的通讯录
创建两个类,一个联系人类,一个电话簿类用来管理联系人
电话本.m文件
@implementation AddressBook
-(instancetype)init{
self = [super self];
if (self) {
_groupDic = [NSMutableDictionary dictionaryWithCapacity:1];
}
return self;
}
//获取名字首字母
- (NSString *)getFirstName:(Contact *)contact{
return[[[contact name] substringToIndex:1] uppercaseString];
}
//根据首字母把联系人加入分组
- (void)addContactByName:(Contact *)contact{
if ([[contact name] length] == 0 || [contact phoneNum] == 0) {
NSLog(@"添加失败");
}else{
NSMutableArray *arr = [_groupDic objectForKey:[self getFirstName:contact]];
if (arr == nil) {[
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:contact];
[_groupDic setValue:arr forKey:[self getFirstName:contact]];
}else{
[arr addObject:contact];
}
}
}
//获取某个分组下得所有联系人,并且按照姓名升序排序(三种方法实现)
//(1),
// - (void)getContactOfGroupAndSorted:(NSString *)groupName{
// NSMutableArray *arr1 = [_groupDic objectForKey:groupName];
// // NSMutableArray *newarr = [_groupDic objectForKey:groupName];
// for (int i = 0; i < [arr1 count] - 1; i++) {
// for (int j = 0; j < [arr1 count] - i - 1; j++) {
// if ([[arr1[j] name] compare:[arr1[j + 1] name]] > 0) {
// [arr1 exchangeObjectAtIndex:j withObjectAtIndex: j + 1];
// }
// }
// }
// }
//(2)
//- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
// NSMutableArray *arr2 = [_groupDic objectForKey:groupName];
// NSComparator myBlock = ^(id obj1, id obj2){
// return [[obj1 name] compare:[obj2 name]];
// };
// [arr2 sortUsingComparator:myBlock];
// NSLog(@"%@", arr2);
// }
//(3)
- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
NSMutableArray *arr3 = _groupDic[groupName];
[arr3 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 name] compare:[obj2 name]];
}];
NSLog(@"%@", arr3);
}
//从通讯录中根据电话号码搜索联系人
- (void)getConctByPhoneNum:(NSUInteger)phoneNum{
NSArray *arr4 = [_groupDic allValues];
for (NSMutableArray *arr in arr4) {
for (Contact *c in arr) {
if ([c phoneNum] == phoneNum) {
NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
}
}
}
}
//获取所以女性联系人,并且按照年龄的降序排列
- (void)getWContactAndSorted:(NSString *)sex{
//获取字典中所有的联系人数组
NSArray *arr5 = [_groupDic allValues];
//用来接受女性联系人
NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:1];
//遍历联系人数组
for (NSArray *a in arr5) {
//遍历联系人数组中的联系人
for (Contact *c in a) {
if ([[c sex] isEqualToString:@"w"]) {
NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
[mArr addObject:c];
}
}
}
//按年龄降序排列
[mArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 age] < [obj2 age]) {
return NSOrderedDescending;
}else{
return NSOrderedSame;
}
}];
}
//根据姓名删除某个联系人
- (void)removeContactByName:(NSString *)name{
//获取名字首字母并转化成大写
NSString *str = [[name substringFromIndex:1] uppercaseString];
for (int i = 0; i < [[_groupDic valueForKey:str] count]; i++) {
if ([[[[_groupDic valueForKey:str] objectAtIndex:i] name] isEqualToString:name]) {
[[_groupDic objectForKey:str] removeObjectAtIndex:i];
//如果对应分组里的联系人为空,将对应的key值也删除
if ([_groupDic objectForKey:str] == nil) {
[_groupDic removeObjectForKey:str];
}
}
}
}
//删除某个分组的全部联系人
- (void)removeAllContactOfGroup:(NSString *)groupName{
[_groupDic removeObjectForKey:groupName];
}
//展示通讯录中的所有联系人
- (void)printAllContactInfo{
NSArray *cArr = [_groupDic allValues];
for (NSMutableArray *arr in cArr) {
for (Contact *contact in arr) {
NSLog(@"%@", contact);
}
}
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Contact *con1 = [[Contact alloc] initWithName:@"Curry" sex:@"m" phoneNum:145 address:@"lanou" age:18];
Contact *con2 = [[Contact alloc] initWithName:@"Aobe" sex:@"w" phoneNum:123 address:@"wanf" age:18];
Contact *con3 = [[Contact alloc] initWithName:@"Bob" sex:@"m" phoneNum:466 address:@"henan" age:18];
AddressBook *AddBook = [[AddressBook alloc] init];
[AddBook addContactByName:con1];
[AddBook addContactByName:con2];
[AddBook addContactByName:con3];
// [AddBook getWContactAndSorted:@"w"];
//[AddBook printAllContactInfo];
[AddBook getConctByPhoneNum:123];
[AddBook printAllContactInfo];
}
return 0;
}
创建两个类,一个联系人类,一个电话簿类用来管理联系人
电话本.m文件
@implementation AddressBook
-(instancetype)init{
self = [super self];
if (self) {
_groupDic = [NSMutableDictionary dictionaryWithCapacity:1];
}
return self;
}
//获取名字首字母
- (NSString *)getFirstName:(Contact *)contact{
return[[[contact name] substringToIndex:1] uppercaseString];
}
//根据首字母把联系人加入分组
- (void)addContactByName:(Contact *)contact{
if ([[contact name] length] == 0 || [contact phoneNum] == 0) {
NSLog(@"添加失败");
}else{
NSMutableArray *arr = [_groupDic objectForKey:[self getFirstName:contact]];
if (arr == nil) {[
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:contact];
[_groupDic setValue:arr forKey:[self getFirstName:contact]];
}else{
[arr addObject:contact];
}
}
}
//获取某个分组下得所有联系人,并且按照姓名升序排序(三种方法实现)
//(1),
// - (void)getContactOfGroupAndSorted:(NSString *)groupName{
// NSMutableArray *arr1 = [_groupDic objectForKey:groupName];
// // NSMutableArray *newarr = [_groupDic objectForKey:groupName];
// for (int i = 0; i < [arr1 count] - 1; i++) {
// for (int j = 0; j < [arr1 count] - i - 1; j++) {
// if ([[arr1[j] name] compare:[arr1[j + 1] name]] > 0) {
// [arr1 exchangeObjectAtIndex:j withObjectAtIndex: j + 1];
// }
// }
// }
// }
//(2)
//- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
// NSMutableArray *arr2 = [_groupDic objectForKey:groupName];
// NSComparator myBlock = ^(id obj1, id obj2){
// return [[obj1 name] compare:[obj2 name]];
// };
// [arr2 sortUsingComparator:myBlock];
// NSLog(@"%@", arr2);
// }
//(3)
- (void)getContactOfGroupAndSorted2:(NSString *)groupName{
NSMutableArray *arr3 = _groupDic[groupName];
[arr3 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 name] compare:[obj2 name]];
}];
NSLog(@"%@", arr3);
}
//从通讯录中根据电话号码搜索联系人
- (void)getConctByPhoneNum:(NSUInteger)phoneNum{
NSArray *arr4 = [_groupDic allValues];
for (NSMutableArray *arr in arr4) {
for (Contact *c in arr) {
if ([c phoneNum] == phoneNum) {
NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
}
}
}
}
//获取所以女性联系人,并且按照年龄的降序排列
- (void)getWContactAndSorted:(NSString *)sex{
//获取字典中所有的联系人数组
NSArray *arr5 = [_groupDic allValues];
//用来接受女性联系人
NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:1];
//遍历联系人数组
for (NSArray *a in arr5) {
//遍历联系人数组中的联系人
for (Contact *c in a) {
if ([[c sex] isEqualToString:@"w"]) {
NSLog(@"%@ %@ %lu %@ %ld", [c name], [c sex], [c phoneNum], [c address], [c age]);
[mArr addObject:c];
}
}
}
//按年龄降序排列
[mArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 age] < [obj2 age]) {
return NSOrderedDescending;
}else{
return NSOrderedSame;
}
}];
}
//根据姓名删除某个联系人
- (void)removeContactByName:(NSString *)name{
//获取名字首字母并转化成大写
NSString *str = [[name substringFromIndex:1] uppercaseString];
for (int i = 0; i < [[_groupDic valueForKey:str] count]; i++) {
if ([[[[_groupDic valueForKey:str] objectAtIndex:i] name] isEqualToString:name]) {
[[_groupDic objectForKey:str] removeObjectAtIndex:i];
//如果对应分组里的联系人为空,将对应的key值也删除
if ([_groupDic objectForKey:str] == nil) {
[_groupDic removeObjectForKey:str];
}
}
}
}
//删除某个分组的全部联系人
- (void)removeAllContactOfGroup:(NSString *)groupName{
[_groupDic removeObjectForKey:groupName];
}
//展示通讯录中的所有联系人
- (void)printAllContactInfo{
NSArray *cArr = [_groupDic allValues];
for (NSMutableArray *arr in cArr) {
for (Contact *contact in arr) {
NSLog(@"%@", contact);
}
}
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Contact *con1 = [[Contact alloc] initWithName:@"Curry" sex:@"m" phoneNum:145 address:@"lanou" age:18];
Contact *con2 = [[Contact alloc] initWithName:@"Aobe" sex:@"w" phoneNum:123 address:@"wanf" age:18];
Contact *con3 = [[Contact alloc] initWithName:@"Bob" sex:@"m" phoneNum:466 address:@"henan" age:18];
AddressBook *AddBook = [[AddressBook alloc] init];
[AddBook addContactByName:con1];
[AddBook addContactByName:con2];
[AddBook addContactByName:con3];
// [AddBook getWContactAndSorted:@"w"];
//[AddBook printAllContactInfo];
[AddBook getConctByPhoneNum:123];
[AddBook printAllContactInfo];
}
return 0;
}
相关文章推荐
- Android Native 绘图方法
- 【DevOps】为什么我们永远疲于奔命?
- 网络管理之IP地址篇
- 文件的读出 编辑 管理
- C#中struct和class的区别详解
- SQL Server 2008 R2 应用及多服务器管理
- VBS ArrayList Class vbs中的数组类
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- VC下通过系统快照实现进程管理的方法
- 深入了解PHP类Class的概念
- jquery 表单验证之通过 class验证表单不为空
- setAttribute 与 class冲突解决
- JavaScript中的类(Class)详细介绍
- javascript面向对象包装类Class封装类库剖析
- jQuery使用hide方法隐藏指定元素class样式用法实例
- jQuery给多个不同元素添加class样式的方法
- jQuery点击改变class并toggle及toggleClass()方法定义用法
- 使用jQuery管理选择结果
- SQLServer 管理常用SQL语句
- JavaScript更改class和id的方法