1》stuID一样时添加失败
2》根据stuID修改联系人姓名、手机号
3》根据姓名删除联系人
1 int main(int argc, const char * argv[]) { 2 @autoreleasepool { 3 4 ManagerPhone *book = [[ManagerPhone alloc]init]; 5 [book creatBookList]; 6 7 NSDictionary *dic1 = [NSDictionary dictionaryWithObjects:@[@"张三",@"15486584587",@"1215"] forKeys:@[@"name",@"phoneNum",@"stuID"]]; 8 9 NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:@[@"张数",@"15486584588",@"1216"] forKeys:@[@"name",@"phoneNum",@"stuID"]]; 10 11 [book addStudent:dic2]; 12 [book addStudent:dic1]; 13 [book replaceStudent:@"1215" withName:@"张达" withPhoneNum:@"1111111111"]; 14 [book deleteStudent:@"小辉"]; 15 16 } 17 return 0; 18 }
1 #import <Foundation/Foundation.h> 2 3 @interface ManagerPhone : NSObject 4 5 - (NSString *)creatBookList; 6 7 - (BOOL)addStudent:(NSDictionary *)stuDic; 8 9 - (BOOL)deleteStudent:(NSString *)stuName; 10 11 - (BOOL)replaceStudent:(NSString *)stuID withName:(NSString *)name withPhoneNum:(NSString *)phoneNum; 12 13 @end
1 @implementation ManagerPhone 2 3 4 - (NSString *)creatBookList 5 { 6 NSFileManager *fileManager = [NSFileManager defaultManager]; 7 NSString *path = @"/Users/scjy/Desktop/刘二龙练习/OC/ManagerPhone/ManagerPhone/ManagerPhone.plist"; 8 if (![fileManager fileExistsAtPath:path]) 9 { 10 BOOL isOk = [fileManager createFileAtPath:path contents:nil attributes:nil]; 11 if (isOk) 12 { 13 NSLog(@"创建成功"); 14 } 15 } 16 else 17 { 18 // NSLog(@"文件已存在,不需要再次创建"); 19 } 20 return path; 21 } 22 23 // 添加 24 - (BOOL)addStudent:(NSDictionary *)stuDic 25 { 26 NSString *path = self.creatBookList; 27 // NSArray *write = [NSArray arrayWithContentsOfFile:path]; 28 // NSMutableArray *array = [[NSMutableArray alloc]init]; 29 30 31 // 读取文件数据到可变数组 32 NSArray *have_array=[NSArray arrayWithContentsOfFile:path]; 33 NSMutableArray *array = [NSMutableArray arrayWithArray:have_array]; 34 35 36 37 38 // NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path]; 39 // 遍历数组,判断联系人是否已添加 40 for (int i = 0; i< array.count; i++) 41 { 42 // 获取数组中字典 43 NSDictionary *dic = [array objectAtIndex:i]; 44 // 判断联系人是否已添加 45 if ([[dic valueForKey:@"stuID"]isEqualToString:[stuDic valueForKey:@"stuID"]]) 46 { 47 NSLog(@"联系人%@已存在,添加失败",[stuDic valueForKey:@"name"]); 48 return NO; 49 } 50 } 51 // 添加联系人到数组 52 [array addObject:stuDic]; 53 54 // 将数组内容写入文件 55 BOOL isSuccess = [array writeToFile:path atomically:YES]; 56 if (isSuccess) { 57 NSLog(@"添加%@成功",[stuDic valueForKey:@"name"]); 58 } 59 return YES; 60 } 61 62 - (BOOL)deleteStudent:(NSString *)stuName 63 { 64 NSString *path = self.creatBookList; 65 // 读取文件数据到可变数组 66 NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path]; 67 // 遍历数组,判断联系人是否已添加 68 for (int i = 0; i< array.count; i++) 69 { 70 // 获取数组中字典 71 NSDictionary *dic = [array objectAtIndex:i]; 72 // 判断联系人是否已添加 73 if ([[dic valueForKey:@"name"]isEqualToString:stuName]) 74 { 75 [array removeObjectAtIndex:i]; 76 [array writeToFile:path atomically:YES]; 77 NSLog(@"联系人%@已存删除",stuName); 78 return YES; 79 } 80 } 81 82 NSLog(@"删除失败"); 83 return NO; 84 85 } 86 87 - (BOOL)replaceStudent:(NSString *)stuID withName:(NSString *)name withPhoneNum:(NSString *)phoneNum 88 { 89 NSString *path = self.creatBookList; 90 // 读取文件数据到可变数组 91 NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path]; 92 // 遍历数组,判断联系人是否已添加 93 for (int i = 0; i< array.count; i++) 94 { 95 // 获取数组中字典 96 NSDictionary *dic = [array objectAtIndex:i]; 97 // 判断联系人是否已添加 98 if ([[dic valueForKey:@"stuID"]isEqualToString:stuID]) 99 { 100 [dic setValue:name forKey:@"name"]; 101 [dic setValue:phoneNum forKey:@"phoneNum"]; 102 [array writeToFile:path atomically:YES]; 103 NSLog(@"联系人%@已存修改",name); 104 return YES; 105 } 106 } 107 108 NSLog(@"删除失败"); 109 return NO; 110 111 } 112 113 114 115 @end