实战(使用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