1, define contact human addresscontact. Instance variables: Name, gender, phone number, address, group name. Method: Customize the initialization method (name, phone number), display contact information
2, define the variable array in main.m, manage the contact person. You can add a new contact object, and if the name or phone number is empty, the print add fails.
3. Get all the contacts under a group.
4. Search for contacts by phone number.
5. Get all female contacts
6. Delete Contacts by name
7. Delete a group all contacts
8. Show all contacts in Address Book
9, choose to do: Define the AddressBook class, encapsulating the above functions.
Main.m
Create an Address Book object
AddressBook * book = [[AddressBook alloc] init];
Addresscontact * P1 = [[Addresscontact alloc] initwithname:@ "Xiao Ming" sex:@ "male" Phonenum: @ "12345566" address:@ "Long Dong da Dao" GroupName : @ "classmate"];
[Book ADDPERSON:P1];
[Book Showaddressbook];
[Book getpersonwithgroupname:@ "classmate"];
[Book removepersonwithgroupname:@ "classmate"];
[Book Showaddressbook];
Create Class-addresscontact.h
#import <Foundation/Foundation.h>
@interface Addresscontact:nsobject
@property (nonatomic,retain) nsstring * name;
@property (nonatomic,retain) nsstring * sex;
@property (nonatomic,retain) NSString * phonenum;
@property (Nonatomic,retain) NSString * address;
@property (nonatomic,retain) NSString * GROUPNAME;
-(ID) Initwithname: (NSString *) name sex: (NSString *) Sex Phonenum: (nsstring*) Phonenum Address: (NSString *) address GroupName: (NSString *) groupName;
1, define contact human addresscontact. Instance variables: Name, gender, phone number, address, group name. Method: Customize the initialization method (name, phone number), display contact information
-(void) information;
@end
. m
#import "AddressContact.h"
@implementation Addresscontact
-(ID) Initwithname: (NSString *) name sex: (NSString *) Sex Phonenum: (nsstring*) Phonenum Address: (NSString *) address GroupName: (NSString *) groupName
{
self = [super init];
if (self) {
_name = name;
_sex = sex;
_phonenum = Phonenum;
_address = address;
_groupname = GroupName;
}
return self;
}
-(void) information
{
NSLog (@ "%@%@%@%@%@", _name,_sex,_phonenum,_address,_groupname);
}
@end
Create Class-addressbook.h
#import <Foundation/Foundation.h>
#import "AddressContact.h"
@interface Addressbook:nsobject
{
Nsmutablearray *_data;
}
2, define the variable array in main.m, manage the contact person. You can add a new contact object, and if the name or phone number is empty, the print add fails.
-(void) Addperson: (Addresscontact *) per;
3. Get all the contacts under a group.
-(void) Getpersonwithgroupname: (NSString *) groupName;
4. Search for contacts by phone number.
-(void) Getpersonwithphonenum: (NSString *) Phonenum;
5. Get all female contacts
-(void) Getpersonwithsex: (NSString *) sex;
6. Delete Contacts by name
-(void) Removeperson: (Addresscontact *) per;
-(void) Removepersonwithname: (NSString *) name;
7. Delete a group all contacts
-(void) Removepersonwithgroupname: (NSString *) groupName;
Show all contact information
-(void) Showaddressbook;
@end
. m
#import "AddressBook.h"
@implementation AddressBook
-(ID) init
{
self = [super init];
if (self) {
_data = [[Nsmutablearray alloc] init];
}
return self;
}
2, define the variable array in main.m, manage the contact person. You can add a new contact object, and if the name or phone number is empty, the print add fails.
-(void) Addperson: (Addresscontact *) per
{
if ([[per name] length] = = 0 | | [[per phonenum] length] = = 0) {
NSLog (@ "Add failed");
Return
}
[_data Addobject:per];
}
3. Get all the contacts under a group.
-(void) Getpersonwithgroupname: (NSString *) groupName
{
For (Addresscontact * p in _data) {
if ([[[P GroupName] isequaltostring:groupname]) {
[P information];
}
}
}
4. Search for contacts by phone number.
-(void) Getpersonwithphonenum: (NSString *) phonenum
{
For (Addresscontact * p in _data) {
if ([[[P Phonenum] isequaltostring:phonenum]) {
[P information];
}
}
}
5. Get all female contacts
-(void) Getpersonwithsex: (NSString *) Sex
{
For (Addresscontact * p in _data) {
if ([[[P Sex] isequaltostring:sex]) {
[P information];
}
}
}
6. Delete Contacts by name
-(void) Removeperson: (Addresscontact *) per
{
[_data Removeobject:per];
}
-(void) Removepersonwithname: (NSString *) name
{
For (Addresscontact * p in _data) {
if ([[[P name] isequaltostring:name]) {
[Self removeperson:p];
}
}
}
7. Delete a group all contacts
-(void) Removepersonwithgroupname: (NSString *) groupName
{
Nsmutablearray * Delete = [Nsmutablearray array];
For (Addresscontact * p in _data) {
if ([[[P GroupName] isequaltostring:groupname]) {
[Delete addobject:p];
}
}
[_data Removeobjectsinarray:delete];
}
8. Show all contacts in Address Book
-(void) Showaddressbook
{
For (Addresscontact * per in _data) {
[Per information];
}
}
@end
Create a simple Address Book