通讯录习题

 1 #import <Foundation/Foundation.h>
 2 // MVC 设计思想
 3 // Model 数据模型
 4 // View 视图
 5 // Controller 控制器
 6 #import "ContactBook.h"
 7 int main(int argc, const char * argv[]) {
 8     @autoreleasepool {
 9         [ContactBook test];
10     }
11     return 0;
12 }
main.m
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Contact : NSObject
 4 @property NSString * name;
 5 @property NSString * nickName;
 6 @property NSString * gender;
 7 @property NSString * phone;
 8 @property NSString * homeTele;
 9 @property NSString * companyTele;
10 @property NSString * company;
11 @property NSString * job;
12 @property NSString * group;
13 @property NSString * companyAddress;
14 @property NSString * homeAddress;
15 @property NSString * birthday;
16 @property NSString * email;
17 @property NSString * qqNumber;
18 @property NSString * msnNumber;
19 @property NSString * remark;
20 
21 
22 
23 
24 @end
Contact.h
1 #import "Contact.h"
2 
3 @implementation Contact
4 
5 - (NSString *)description{
6     return [NSString stringWithFormat:@"name:%@, nickName:%@, gender:%@, phone:%@, homeTele:%@, companyTele:%@, company:%@, job:%@, group:%@, companyAddress:%@, homeAddress:%@, birthday:%@, email:%@, qqNumber:%@, msnNumber:%@, remark:%@",_name,_nickName,_gender,_phone,_homeTele,_companyTele,_company,_job,_group,_companyAddress,_homeAddress,_birthday,_email,_qqNumber,_msnNumber,_remark];
7 }
8 @end
Contact.m
1 #import <Foundation/Foundation.h>
2 
3 @interface ContactBook : NSObject {
4     NSMutableArray * _array;
5 }
6 + (void)test;
7 - (void)addContacts;
8 - (void)showInfo;
9 @end
ContactBook.h
 1 #import "ContactBook.h"
 2 #import "Contact.h"
 3 #define PATH @"/Users/wangqitai/Desktop/归档/contacts.txt"
 4 #define SHOW(a) NSLog(@"%@",a);
 5 @implementation ContactBook
 6 - (id)init {
 7     if (self = [super init]) {
 8         _array = [[NSMutableArray alloc] init];
 9         [self addContacts];
10     }
11     return self;
12 }
13 
14 - (void)addContacts {
15     // 1 读取文件中的字符串
16     NSError * a;
17     
18     NSString * str = [NSString stringWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:&a];
19 //    NSLog(@"%@",str);
20     NSArray * contacts = [str componentsSeparatedByString:@"
"];
21 //    SHOW(contacts);
22     for (int i = 1; i<contacts.count; i++) {
23         NSString * contactStr = contacts[i];
24         NSArray * contact = [contactStr componentsSeparatedByString:@","];
25         Contact * cont = [[Contact alloc] init];
26         cont.name = contact[0];
27         cont.nickName = contact[1];
28         cont.gender = contact[2];
29         cont.phone = contact[3];
30         cont.homeTele = contact[4];
31         cont.companyTele = contact[5];
32         cont.company = contact[6];
33         cont.job = contact[7];
34         cont.group = contact[8];
35         cont.companyAddress = contact[9];
36         cont.homeAddress = contact[10];
37         cont.birthday = contact[11];
38         cont.email = contact[12];
39         cont.qqNumber = contact[13];
40         cont.msnNumber = contact[14];
41         cont.remark = contact[15];
42         // 将每个联系人保存到数组
43         [_array addObject:cont];
44     }
45 }
46 
47 - (void)showInfo {
48     for ( Contact * con in _array) {
49         NSLog(@"%@",con);
50     }
51 }
52 
53 + (void)test {
54     ContactBook * contactBook = [[ContactBook alloc] init];
55     [contactBook showInfo];
56 }
57 
58 @end
ContactBook.m

联系人以换行分隔      联系人中的属性以,分隔

原文地址:https://www.cnblogs.com/gwkiOS/p/4934667.html