实战(使用OC的知识制作一个简易通讯录)
//语法糖、笑笑语法
// NSString * string = [NSString stringWithFormat:@"string"];
NSString * str = @"string";
NSLog(@"%@",str);
//上面的两种写法是一样的,是字符串的简单写法
// NSArray * arr = [NSArray arrayWithObjects:@"yuansu",@"yuansu2", nil];
NSArray * arr1 = @[@"",@""];
// NSLog(@"%@",dic);
NSDictionary * dic1 =@{@"key0":@"value0",@"key1":@"value1",@"key2":@"value2"};
//笑笑语法创建的集合都是不可变的对象
//不可变转变为可变
NSMutableDictionary * dic = [dic1 mutableCopy];
NSMutableArray * arr = [arr1 mutableCopy];
NSMutableString * strr = [str mutableCopy];
NSLog(@"%@ %@ %@",dic,arr,strr);

笑笑语法

       下面写法都是一样的代表判断为空
if (str == nil) {
<#statements#>
}
if (!str) {
<#statements#>
}
*/ /*
array1 == nil; 数组的对象的空间没有开辟 没有这个对象
[array count] == 0; 数组对象存在,只是数组对象里面没有一个元素
*/ /*
str = nil; 对象为空
[str length] == 0;数组存在,但是没有元素
所以以后使用对象的时候,一定要为对象开辟空间,特别是数组字典这种集合,在进行存值或者其他操作时候,不要忘记开辟空间
*/ /*
NSMutableDictionary * dic = @{@"key1":@"value1",@"key2":@"value2"};
dic [@"key1"];//等于写法:[dic objectForKey:@"key1"]
arr [1];//等于写法:[arr objectAtInde:1]
*/

是否为空的判断

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
printf("创建一个26个数组分别对应一个字典的key值\n");
NSMutableArray * a = [[NSMutableArray alloc]initWithObjects:@"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];
NSMutableDictionary * aDic = [[NSMutableDictionary alloc]initWithCapacity:[a count]];
for (NSString *p in a) {
NSMutableArray *arr = [NSMutableArray array];
[aDic setObject:arr forKey:p];
}
NSLog(@"%@",aDic);
}
return ;
}

创建一个字典有26个键分别为A-Z

通讯录实战:

#import <Foundation/Foundation.h>
#import "Contact.h"
#import "AddressBook.h" int main(int argc, const char * argv[]) {
@autoreleasepool { AddressBook * book = [[AddressBook alloc]init];
//添加联系人
Contact *lanou39 = [[Contact alloc]initWithName:@"AAA" gender:@"man" phonenum:@"" address:@"uc"];
Contact *lanou38 = [[Contact alloc]initWithName:@"BBB" gender:@"gril" phonenum:@"" address:@"china"];
//forin 也叫快速枚举,就是遍历容器或者遍历集合,在遍历的过程中容器中不能修改,如果需修改可用for循环替代
[book addContact:lanou39];
[book addContact:lanou38];
[book displayAllContacs];
[book deleteContactWithName:@"niujianwei"];
[book deleteContactWithName:@"Yingjie"];//如果删除有同名的,只能删除一个
NSMutableArray * forAll = [book allContactWithGroupName:@"N"];
NSLog(@"获取N分组的联系人:%@",forAll);
[book displayAllContacs];
NSMutableArray * grilarr = [book getAllFemaContact];
NSLog(@"所有的女性为 :%@",grilarr);
Contact * phone = [book getContactWithPhoneNum:@""];
NSLog(@"电话为13523526302的联系人信息为:%@",phone); /*可删代码(所有的方法都在main函数里写的)
// //添加联系人
// Contact * newPer = [[Contact alloc]initWithName:@"new" gender:@"man" phonenum:@"1245879" address:@"newPlace"];
// NSString * name = [newPer name];
// NSString * phonenum = [newPer phonenmu];
// if (!name || ![name length]|| !phonenum || ![phonenum length]) {
// NSLog(@"添加失败");
// }
// else{
// //获取名字首字母
// NSString * firstChar = [[name substringToIndex:1]capitalizedString];
// //获取对应分组
// NSMutableArray * groupArr = [contactDic objectForKey:firstChar];
// if (!groupArr) {
// //如果分组不存在,添加对应的分组
// groupArr = [NSMutableArray array];
// [contactDic setObject:groupArr forKey:firstChar];
// }
// //添加新的联系人
// [groupArr addObject:newPer];
// }
// NSLog(@"%@",contactDic);
// //获取某个分组的所有联系人(Y)
// NSMutableArray * arr = [contactDic objectForKey:@"Y"];
// NSLog(@"Y分组联系人:%@",arr);
// //根据电话号码搜索联系人
// for (NSString * key in contactDic) {
// NSMutableArray * groupArr = [contactDic objectForKey:key];
// for (Contact *contact in groupArr) {
// if ([[contact phonenmu] isEqualToString:@"1245879"]) {
// NSLog(@"%@",contact);
// }else{
// NSLog(@"没有此人");
// }
// }
// }
// //找到所有女性联系人
// NSMutableArray * grilArr = [NSMutableArray array];
// for (NSString * key in contactDic) {
// NSMutableArray * groupArr = [contactDic objectForKey:key];
// for (Contact * contact in groupArr) {
// if ([[contact gender]isEqualToString:@"gril"]) {
// [grilArr addObject:contact];
// }
// }
// }
// NSLog(@"女性联系人:%@",grilArr);
// //根姓名删除联系人
// for (NSString * key in contactDic) {
// NSMutableArray * group = contactDic[key];
// //遍历联系人分组
// for (int i = 0; i < group.count; i++) {
// Contact * contact = group[i];
// if ([[contact name] isEqualToString:@"niu"]) {
// //从对应分组中删除联系人
// [group removeObjectAtIndex:i];//打方法的时候要注意刚才就写错了
// break;
// }
// }
// }
// //删除某个分组的联系人
// [contactDic removeObjectForKey:@"X"];
// NSLog(@"%@",contactDic);
// //展示所有联系人
// NSLog(@"%@",contactDic);//打印字典所有的联系人都会打印
// //
*/
}
return ;
}
mian()函数文件

main

#import <Foundation/Foundation.h>

@interface Contact : NSObject
{
NSString * _name;
NSString * _gender;
NSString * _phonenum;
NSString * _address;
NSString * _groupName;
}
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setGender:(NSString *)gender;
- (NSString *)gender;
- (void)setPhoneNmu:(NSString *)phonenum;
- (NSString *)phonenmu;
- (void)setAddress:(NSString *)address;
- (NSString *)address;
- (void)setGroupName:(NSString *)GroupName;
- (NSString *)GroupName; - (id)initWithName:(NSString *)name
gender:(NSString *)gender
phonenum:(NSString *)phonenum
address:(NSString *)address;
//显示信息
- (void)displayContactInfo;
//描述
- (NSString *)description;
@end

Contact.h

#import "Contact.h"

@implementation Contact
- (void)setName:(NSString *)name{
_name = name;
}
- (NSString *)name{
return _name;
}
- (void)setGender:(NSString *)gender{
_gender = gender;
}
- (NSString *)gender{
return _gender;
}
- (void)setPhoneNmu:(NSString *)phonenum{
_phonenum = phonenum;
}
- (NSString *)phonenmu{
return _phonenum;
}
- (void)setAddress:(NSString *)address{
_address = address;
}
- (NSString *)address{
return _address;
}
- (void)setGroupName:(NSString *)GroupName{
_groupName = GroupName;
}
- (NSString *)GroupName{
return _groupName;
} - (id)initWithName:(NSString *)name
gender:(NSString *)gender
phonenum:(NSString *)phonenum
address:(NSString *)address{
self = [super init];
if (self) {
_name = name;
_address = address;
_phonenum = phonenum;
_gender = gender; }
return self;
}
- (void)displayContactInfo{
NSLog(@"姓名:%@ 性别:%@ 电话:%@ 地址:%@ 分组 %@",_name,_gender,_phonenum,_address,_groupName);
}
- (NSString *)description{
return [NSString stringWithFormat:@"%@ %@ %@ %@ %@",_name,_gender,_address,_phonenum,_groupName];
}
@end

Contact.m

#import <Foundation/Foundation.h>
#import "Contact.h" //接口(就是声明的一些方法)
@interface AddressBook : NSObject
//getter
- (NSMutableDictionary *)allContacts;
//添加新的联系人
- (void)addContact:(Contact *)contact;
//获取某个分组的联系人
- (NSMutableArray * )allContactWithGroupName:(NSString *)groupName;
//根据电话号码搜索联系人
- (Contact *)getContactWithPhoneNum:(NSString *)phoneNum;
//获取所有的女性联系人
- (NSMutableArray *)getAllFemaContact;
//根据姓名删除联系人
- (void)deleteContactWithName:(NSString *)name;
//删除某个分组的全部联系人
- (void)deleteAllContactsWithGroupName:(NSString *)groupName;
//展示通讯录中的所有联系人
- (void)displayAllContacs;
//初始化重写
- (id)init;
//自定义初始化方法
- (id)initWithDic;
@end

AddressBook.h

#import "AddressBook.h"

@implementation AddressBook
{
NSMutableDictionary * _allContacts;//存储所有的联系人
}
//getter
- (NSMutableDictionary *)allContacts{
return _allContacts;
}
//自定义初始化方法
- (id)initWithDic{
self = [super init];
if (self) {
Contact *yingjie = [[Contact alloc]initWithName:@"Yingjie" gender:@"man" phonenum:@"" address:@"US" ];
Contact *ying = [[Contact alloc]initWithName:@"Ying" gender:@"man" phonenum:@"" address:@"US" ];
Contact *zhang = [[Contact alloc]initWithName:@"Zhang" gender:@"man" phonenum:@"" address:@"US" ];
Contact *huang = [[Contact alloc]initWithName:@"Huang" gender:@"man" phonenum:@"" address:@"US" ];
Contact *liu = [[Contact alloc]initWithName:@"Liuxian" gender:@"gril" phonenum:@"" address:@"US" ];
Contact *niu = [[Contact alloc]initWithName:@"Niu" gender:@"man" phonenum:@"" address:@"US" ];
//创建分组
NSMutableArray * yArr = [NSMutableArray arrayWithObjects:yingjie,ying, nil];
NSMutableArray * zArr = [NSMutableArray arrayWithObjects:zhang, nil];
NSMutableArray * hArr = [NSMutableArray arrayWithObjects:huang, nil];
NSMutableArray * lArr = [NSMutableArray arrayWithObjects:liu, nil];
NSMutableArray * nArr = [NSMutableArray arrayWithObjects:niu, nil];
//把分组名称放到对应的字典(管理所有分组联系人)
_allContacts = [NSMutableDictionary dictionaryWithObjectsAndKeys:yArr,@"Y",zArr,@"Z",hArr,@"H",lArr,@"",nArr,@"N",lArr,@"L", nil ]; }
return self;
}
//添加新的联系人
- (void)addContact:(Contact *)contact{
if (![contact name]||![[contact name]length]|| ![contact phonenmu]||![[contact phonenmu]length]) {
NSLog(@"添加失败");
}else{
NSString * name = [contact name];
NSString * fristCahr = [[name substringToIndex:]capitalizedString];
NSMutableArray * group = [_allContacts objectForKey:fristCahr];
if (!group) {
group = [NSMutableArray array];
[group addObject:contact];
[_allContacts setObject:group forKey:fristCahr];
}
[group addObject:contact];
}
} //获取某个分组的联系人
- (NSMutableArray * )allContactWithGroupName:(NSString *)groupName{
return [_allContacts objectForKey:groupName];
}
//根据电话号码搜索联系人
- (Contact *)getContactWithPhoneNum:(NSString *)phoneNum{
for (NSString * key in _allContacts) {
NSMutableArray * group = _allContacts[key];
for (Contact *contact in group) {
if ([[contact phonenmu] isEqualToString:phoneNum]) {
return contact;
}
}
}
return nil;
}
//获取所有的女性联系人
- (NSMutableArray *)getAllFemaContact{
NSMutableArray * girlArr = [NSMutableArray array];
for (NSString * key in _allContacts) {
NSMutableArray * group = _allContacts[key];
for (Contact * contact in group) {
if ([[contact gender]isEqualToString:@"gril"]) {
[girlArr addObject:contact];
return contact;
}
}
}
return nil;
}
//根据姓名删除联系人
- (void)deleteContactWithName:(NSString *)name{
//获取姓名的首字母
NSString * first = [[name substringToIndex:]capitalizedString];
//获取联系人分组
NSMutableArray * group = [_allContacts objectForKey:first];
for (Contact *contact in group) {//不能对容器做更改
if ([[contact name]isEqualToString:name]) {
[group removeObject:contact];//如果是我们要找的人那就删除
NSLog(@"删除一条数据成功");
break;
}
}
}
//删除某个分组的全部联系人
- (void)deleteAllContactsWithGroupName:(NSString *)groupName{
[_allContacts removeObjectForKey:groupName];
NSLog(@"成功删除分组%@",groupName);
}
//展示通讯录中的所有联系人
- (void)displayAllContacs{
NSLog(@"%@",_allContacts);
}
//重写init 方法
- (id)init{
self = [super init];
if (self) {
//存储联系人
//建立联系人对象
Contact *yingjie = [[Contact alloc]initWithName:@"Yingjie" gender:@"man" phonenum:@"" address:@"US" ];
Contact *ying = [[Contact alloc]initWithName:@"Ying" gender:@"man" phonenum:@"" address:@"US" ];
Contact *zhang = [[Contact alloc]initWithName:@"Zhang" gender:@"man" phonenum:@"" address:@"US" ];
Contact *huang = [[Contact alloc]initWithName:@"Huang" gender:@"man" phonenum:@"" address:@"US" ];
Contact *liu = [[Contact alloc]initWithName:@"Liuxian" gender:@"gril" phonenum:@"" address:@"US" ];
Contact *niu = [[Contact alloc]initWithName:@"Niu" gender:@"man" phonenum:@"" address:@"US" ];
//创建分组
NSMutableArray * yArr = [NSMutableArray arrayWithObjects:yingjie,ying, nil];
NSMutableArray * zArr = [NSMutableArray arrayWithObjects:zhang, nil];
NSMutableArray * hArr = [NSMutableArray arrayWithObjects:huang, nil];
NSMutableArray * lArr = [NSMutableArray arrayWithObjects:liu, nil];
NSMutableArray * nArr = [NSMutableArray arrayWithObjects:niu, nil];
//把分组名称放到对应的字典(管理所有分组联系人)
_allContacts = [NSMutableDictionary dictionaryWithObjectsAndKeys:yArr,@"Y",zArr,@"Z",hArr,@"H",lArr,@"L",nArr,@"N", nil ];
}
return self;
}
@end

AddressBook.m

OC:通讯录实战的更多相关文章

  1. OC通讯录选择封装

    ContactsService.h代码 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> //block返回选 ...

  2. h5-29-WEB存储-通讯录实战.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 16-6 WEB存储-通讯录实战

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. iOS 之 OC开发实战

    iOS 开发之登陆 iOS 程序初始一个带导航栏的视图 iOS 添加导航栏两侧按钮 iOS UITabBar

  5. 实战项目:通讯录&nbsp;UI—第十一天

     1.推出视图的两种方式:  1.通过导航控制器push到下一个界面,使用pop返回到上一个界面 2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismi ...

  6. Swift 简单的通讯录

    Swift 通讯录实战 1.功能需求 整个项目由三个界面构成:首页面(全部联系人),添加联系人界面和联系人详情界面 整个项目使用纯代码编程 数据处理方面使用一个工具类,处理所有数据的增删改查. 首页由 ...

  7. iOS 程序开发

    准备 iOS 开发 之 编程知识点 iOS 程序调试 iOS 之 OC开发实战 iOS 架构模式 iOS 之 新功能.扩展

  8. WEB学习路线2019完整版(附视频教程+网盘下载地址)

    WEB学习路线2019完整版(附视频教程+网盘下载地址).适合初学者的最新WEB前端学习路线汇总! 在当下来说web前端开发工程师可谓是高福利.高薪水的职业了.所以现在学习web前端开发的技术人员也是 ...

  9. iOS开发——高级技术精选OC篇&Runtime之字典转模型实战

    Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...

随机推荐

  1. php里少用到的session_module_name,以及session的key值限制,简单将session存储为json格式数据的方法

    这个函数的作用就是动态的设置php.ini里的session_save_handler,配合session_set_savepath可以在程序里自由配置session的后台方式. session_ca ...

  2. 【英语】Bingo口语笔记(60) - 口语中的浊化发音

  3. Android下实现win8的按钮点击效果

    原理就是自定义一个imageButton,实现动画效果 demo源码下载地址:  请戳这里----------------> 关于回弹张力的效果扩展,可以参考Facebook的开源动画库rebo ...

  4. Notify通知

    1.NotificationManager类对象         <1>getSystemService(Context.NOTIFICATION_SERVICE) 获取通知管理对象   ...

  5. ADO.NET+Access: 2,至少一个参数没有被指定值

    ylbtech-Error-ADO.NET+Access: 2,至少一个参数没有被指定值. 1.A,错误代码返回顶部  2,至少一个参数没有被指定值. 1.B,出错原因分析返回顶部  未解决 1.C, ...

  6. 【剑指offer 面试题38】数字在排序数组中出现的次数

    思路: 利用二分查找,分别查找待统计数字的头和尾的下标,最后做差加一即为结果. C++: #include <iostream> #include <vector> using ...

  7. C++模板详解

    参考:C++ 模板详解(一) 模板:对类型进行参数化的工具:通常有两种形式: 函数模板:仅参数类型不同: 类模板:   仅数据成员和成员函数类型不同. 目的:让程序员编写与类型无关的代码. 注意:模板 ...

  8. C语言实现strcat

    首先看看代码: #ifndef STRCAT_H #define STRCAT_H /********************************************************* ...

  9. php 高效分页

    mysql.php 获取数据库中的记录,完全个人经验总结,仅供参考!<?php/***PHP+MYSQL数据库基本功能*http://blog.csdn.net/yown*/########## ...

  10. Javascript类型转换表

    各种类型的值 转换为各种类型 String Number Boolean Object undefined "undefined" NaN false 报错 null " ...