Create a simple Address Book

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.