Objective-c Address Book instance (using nsstring, nsarray, etc)

Source: Internet
Author: User
Tags dname

 

An example of learning the Foundation has used all the things mentioned above, which is not bad.

Code:

 

// The AddressCard interface declares the header file

AddressCard. h

# Import <Foundation/NSObject. h> <br/> # import <Foundation/NSString. h> </p> <p> @ interface AddressCard: NSObject <br/>{< br/> NSString * name; <br/> NSString * email; <br/>}</p> <p>-(void) setName: (NSString *) theName; <br/>-(void) setEmail: (NSString *) theEmail; <br/>-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail; </p> <p>-(NSString *) name; <br/>-(NSString *) email; </p> <p>-(void) print; <br/>-(NSComparisonResult) compareNames: (id) element; <br/>-(void) dealloc; <br/> @ end <br/>

 

 

// AddressCard interface implementation file

AddressCard. m

# Import "addresscard. H "</P> <p> @ implementation addresscard <br/>-(void) setname: (nsstring *) thename <br/> {<br/> // release old object before dispatch a new one <br/> // (even when name is nil, you also can send it a message) <br/> [Name Release]; <br/> name = [[nsstring alloc] initwithstring: thename]; <br/>}</P> <p>-(void) setemail: (nsstring *) theemail <br/>{< br/> // release old object before dispatch a new one <br/> [email release]; <br/> email = [[nsstring alloc] initwithstring: theemail]; <br/>}</P> <p>-(nsstring *) name <br/>{< br/> return name; <br/>}</P> <p>-(nsstring *) email <br/>{< br/> return email; <br/>}</P> <p>-(void) setname: (nsstring *) thename andemail: (nsstring *) theemail <br/>{< br/> [email release]; <br/> [Name Release]; <br/> email = [[nsstring alloc] initwithstring: theemail]; <br/> name = [[nsstring alloc] initwithstring: thename]; </P> <p> name = thename; <br/> email = theemail; <br/>}</P> <p>-(void) print <br/> {<br/> nslog (@ "========================== ============ "); <br/> nslog (@ "|"); <br/> nslog (@ "| %-31 S |", [name utf8string]); <br/> nslog (@ "| %-31 S |", [email utf8string]); <br/> nslog (@ "| "); <br/> nslog (@ "|"); <br/> nslog (@ "| "); <br/> nslog (@ "| o | "); <br/> nslog (@ "================================== ===== "); <br/>}</P> <p> // comprae the two names from the specified address cards <br/>-(nscomparisonresult) comparenames: (ID) element <br/>{< br/> return [name compare: [element name]; <br/>}</P> <p> // for release the object name and email <br/>-(void) dealloc <br/>{< br/> [Name Release]; <br/> [email release]; <br/> [Super dealloc]; <br/>}< br/> @ end <br/>

 

// AddressBook interface declaration File

AddressBook. h

# Import <Foundation/NSArray. h> <br/> # import "AddressCard. h "</p> <p> @ interface AddressBook: NSObject <br/>{< br/> NSString * bookName; <br/> NSMutableArray * book; <br/>}</p> <p>-(id) initWithName: (NSString *) name; <br/>-(void) addCard: (AddressCard *) theCard; <br/>-(int) entries; <br/>-(void) list; <br/>-(void) sort; <br/>-(AddressCard *) lookup: (NSString *) theName; <br/>-(void) dealloc; </p> <p> @ end <br/>

 

// AddressBook interface implementation file

AddressBook. m

# Import "addressbook. H "</P> <p> @ implementation addressbook <br/> // set up the addressbook's name and an empty book <br/>-(ID) initwithname: (nsstring *) name <br/>{< br/> Self = [Super init]; </P> <p> If (Self) <br/> {<br/> bookname = [[nsstring alloc] initwithstring: Name]; <br/> book = [[nsmutablearray alloc] init]; <br/>}</P> <p> // because if this class has a subclass and IT use this method to initialise, <br/> // The self will not be a addressbook object, <br/> // so we return a (ID) object other than a addressbook object <br/> return self; </P> <p >}</P> <p>-(void) addcard :( addresscard *) thecard <br/>{< br/> // Add card to the mutable array <br/> [Book addobject: thecard]; <br/>}</P> <p>-(INT) entries <br/>{< br/> // count the num of address card <br/> return [Book count]; <br/>}</P> <p> // lookup address card by name-assumes and exact match <br/>-(addresscard *) lookup: (nsstring *) thename <br/> {<br/> for (INT I = 0; I <[Book count]; I ++) <br/> {<br/> If ([[Book objectatindex: I] Name] caseinsensitivecompare: thename] = nsorderedsame) <br/>{< br/> return [Book objectatindex: I]; <br/>}< br/> return nil; <br/>}</P> <p>-(void) list <br/> {<br/> nslog (@ "========= contents of: % ======== ", bookname); <br/> // quickly Enum <br/> // For (addresscard * thecard in Book) <br/> // nslog (@ "%-20 S %-32 s", [thecard. name utf8string], [thecard. email utf8string]); <br/> for (INT I = 0; I <[Book count]; ++ I) <br/>{< br/> nslog (@ "%-20 S %-32 s", [[Book objectatindex: I] Name] utf8string], [[Book objectatindex: I] email] utf8string]); <br/>}</P> <p> nslog (@ "======================== ============================== "); <br/>}</P> <p>-(void) Sort <br/>{< br/> [Book sortusingselector: @ selector (comparenames :)]; <br/>}</P> <p> // for deep release <br/>-(void) dealloc <br/>{< br/> [bookname release]; <br/> [book release]; <br/> [Super dealloc]; <br/>}< br/> @ end <br/>

 

// Main program file

AddressMain. m

# Import "AddressBook. h "<br/> # import <Foundation/NSAID utoreleasepool. h> </p> <p> int main (int argc, const char * argv []) <br/> {<br/> NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init]; <br/> NSString * aName = @ "Julia Kochan "; <br/> NSString * aEmail = @ "jewls337@axlc.com"; <br/> NSString * bName = @ "Tony Iannino"; <br/> NSString * bEmail = @ "tony.iannino@teachfitnees.com "; <br/> NSString * cName = @ "S Tephen Kochan "; <br/> NSString * cEmail = @" steve@kochan-wood.com "; <br/> NSString * dName = @" Jamie Baker "; <br/> NSString * dEmail = @ "jbaker@kochan-wood.com"; </p> <p> AddressCard * card1 = [[AddressCard alloc] init]; <br/> AddressCard * card2 = [[AddressCard alloc] init]; <br/> AddressCard * card3 = [[AddressCard alloc] init]; <br/> AddressCard * card4 = [[AddressCard alloc] init]; </p> <p> AddressBook * MyBook = [AddressBook alloc]; <br/> AddressCard * myCard; </p> <p> // Firest set up four address cards <br/> [card1 setName: aName andEmail: aEmail]; <br/> [card2 setName: bName andEmail: bEmail]; <br/> [card3 setName: cName andEmail: cEmail]; <br/> [card4 setName: dName andEmail: dEmail]; </p> <p> // Now initialize the address book <br/> myBook = [myBook initWithName: @ "Linda's Address Book"]; <br/> NSL Og (@ "Entries in address book after creation: % I", [myBook entries]); </p> <p> // Add some cards to the address book <br/> [myBook addCard: card1]; <br/> [myBook addCard: card2]; <br/> [myBook addCard: card3]; <br/> [myBook addCard: card4]; </p> <p> NSLog (@ "Entries in the address book after adding cards: % I", [myBook entries]); </p> <p> // List all the entries in the book now <br/> [myBook list]; </p> <p>/So Rt it and list it again <br/> [myBook sort]; <br/> [myBook list]; </p> <p> // Lookup a person by name <br/> NSLog (@ "Look up: Stephen Kochan"); <br/> myCard = [myBook lookup: @ "stephen kochan"]; </p> <p> if (myCard! = Nil) <br/> [myCard print]; <br/> else <br/> NSLog (@ "Not found! "); </P> <p> [card1 release]; <br/> [card2 release]; <br/> [card3 release]; <br/> [card4 release]; <br/> [myBook release]; <br/> [pool drain]; <br/> return 0; <br/>}< br/>

 

 

Related Article

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.