ProgrammingInObjectiveC:Appendix C

From 흡혈양파의 번역工房
Jump to navigation Jump to search
부록 C
Address Book 소스코드

부록 C :: Address Book 소스코드

2부 「Foundation 프레임워크」에서 작업했던 주소록 예제의 인터페이스 파일과 구현 파일을 편하게 참고하도록 소스코드 전체를 이곳에 싣는다. AddressCard, AddressBook 클래스 정의가 포함된다. 이들 클래스를 여러분 시스템에 구현하고, 확장해서 더 강력하고 실용적인 프로그램을 작성해 보자. 그 과정에서 Objective-C 언어를 학습하고, 프로그램 생성하기, 클래스 및 객체 다루기, Foundation 프레임워크 작업하기 등의 작업에 친숙해질 것이다.


AddressCard 인터페이스 파일

#import <Foundation/Foundation.h>

@interface AddressCard : NSObject <NSCopying, NSCoding> {
  NSString *name;
  NSString *email;
}

@property (nonatomic, copy) NSString *name, *email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) retainName: (NSString *) theName andEmail: (NSString *) theEmail;
-(NSComparisonResult) compareNames: (id) element;

-(void) print;

@end


AddressBook 인터페이스 파일

#import <Foundation/Foundation.h>
#import "AddressCard.h"

@interface AddressBook: NSObject <NSCopying, NSCoding>
{
  NSString *bookName;
  NSMutableArray *book;
}

@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, copy) NSMutableArray *book;

-(id) initWithName: (NSString *) name;
-(void) sort;
-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(AddressCard *) lookup: (NSString *) theName;

-(void) dealloc;

@end


AddressCard 구현 파일

#import "AddressCard.h"

@implementation AddressCard

@synthesize name, email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
  [self setName: theName];
  [self setEmail: theEmail];
}

// 지정한 주소 카드의 이름을 비교한다.

-(NSComparisonResult) compareNames: (id) element
{
  return [name compare: [element name]];
}

-(void) print
{
  NSLog (@"====================================");
  NSLog (@"|                                  |");
  NSLog (@"|  %-31s   |", [name UTF8String]);
  NSLog (@"|  %-31s   |", [email UTF8String]);
  NSLog (@"|                                  |");
  NSLog (@"|                                  |");
  NSLog (@"|                                  |");
  NSLog (@"|          0                0      |");
  NSLog (@"====================================");
}

-(AddressCard *) copyWithZone: (NSZone *) zone
{
  AddressCard *newCard = [[AddressCard allocWithZone: zone] init];

  [newCard retainName: name andEmail: email];
  return newCard;
}

-(void) retainName: (NSString *) theName andEmail: (NSString *) theEmail
{
  name = [theName retain];
  email = [theEmail retain];
}

-(void) encodeWithCoder: (NSCoder *) encoder
{
  [encoder encodeObject: name forKey: @"AddressCardName"];
  [encoder encodeObject: email forKey: @"AddressCardEmail"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
  name = [[decoder decodeObjectForKey: @"AddressCardName"] retain];
  email = [[decoder decodeObjectForKey: @"AddressCardEmail"] retain];

  return self;
}

-(void) dealloc
{
  [name release];
  [email release];
  [super dealloc];
}
@end


AddressBook 구현 파일

#import "AddressBook.h"

@implementation AddressBook

@synthesize book, bookName;

// 빈 주소록을 생성하고 이름을 설정한다.

-(id) initWithName: (NSString *) name{
  self = [super init];

  if (self) {
    bookName = [[NSString alloc] initWithString: name];
    ok = [NSMutableArray alloc] init];
}

return self;
}

-(void) sort
{
  [book sortUsingSelector: @selector(compareNames:)];
}

-(void) addCard: (AddressCard *) theCard
{
  [book addObject: theCard];
}

-(void) removeCard: (AddressCard *) theCard
{
  [book removeObjectIdenticalTo: theCard];
}

-(int) entries
{
  return [book count];
}

-(void) list
{
  NSLog (@"======== Contents of: %@ =========", bookName);

  for ( AddressCard *theCard in book )
    NSLog (@"%-20s %-32s", [theCard.name UTF8String],
	   [theCard.email UTF8String]);
  NSLog (@"====================================");
}

// 이름으로 주소록 검색. 정확히 일치하는 이름을 찾는다.

-(AddressCard *) lookup: (NSString *) theName
{
  for ( AddressCard *nextCard in book )
    if ( [[nextCard name] caseInsensitiveCompare: theName] == NSOrderedSame )
      return nextCard;

  return nil;
}

-(void) dealloc
{
  [bookName release];
  [book release];
  [super dealloc];
}

-(void) encodeWithCoder: (NSCoder *) encoder
{
  [encoder encodeObject:bookName forKey: @"AddressBookBookName"];
  encoder encodeObject:book forKey: @"AddressBookBook"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
  bookName = [[decoder decodeObjectForKey: @"AddressBookBookName"] retain];
  book = [[decoder decodeObjectForKey: @"AddressBookBook"] retain];

  return self;
}

// NSCopying 프로토콜의 메서드

-(id) copyWithZone: (NSZone *) zone
{
  AddressBook *newBook = [[self class] allocWithZone: zone];

  [newBook initWithName: bookName];
  [newBook setBook: book];

  return newBook;
}
@end


Notes