简单地址簿?

 

AddressCard.h

#import <Foundation/Foundation.h>

@interface AddressCard : NSObject
{
    NSString *_name;
    NSString *_eMail;
}
@property(nonatomic,copy)NSString *name,*eMail;
//-(void)setName:(NSString *)aName;
//-(NSString *)name;
//-(void)setEMail:(NSString *)aEMail;
//-(NSString *)eMail;
-(void)setName:(NSString *)aName
      andEmail:(NSString *)aEmail;
-(void)print;
-(NSComparisonResult)compareName:(id)element;
@end

AddressCard.m

#import "AddressCard.h"

@implementation AddressCard
/*
-(void)setName:(NSString *)aName
{
    if (_name != aName)
    {
        _name = [NSString stringWithString:aName];
    }
}
-(NSString *)name
{
    return _name;
}

-(void)setEMail:(NSString *)aEMail
{
    if(_eMail != aEMail)
    {
        _eMail = [NSString stringWithString:aEMail];
    }
}
-(NSString *)eMail
{
   return _eMail;
}
*/
@synthesize name = _name,eMail = _eMail;
-(void)setName:(NSString *)aName
      andEmail:(NSString *)aEmail
{
    self.name = aName;
    self.eMail = aEmail;
}
-(void)print
{
    NSLog(@"===================================");
    NSLog(@"|                                 |");
    NSLog(@"| %-31s |",[_name UTF8String]);
    NSLog(@"| %-31s |",[_eMail UTF8String]);
    NSLog(@"|                                 |");
    NSLog(@"|                                 |");
    NSLog(@"|                                 |");
    NSLog(@"|           0          0          |");
    NSLog(@"===================================");
}

-(NSComparisonResult)compareName:(id)element
{
    return [_name compare:[element name]];
}
//重写
-(BOOL)isEqual:(AddressCard *)theCard
{
    if ([_name isEqualToString:theCard.name] == YES
        )//&& [_eMail isEqualToString:theCard.eMail] == YES
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end

AddressBook.h

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

@interface AddressBook : NSObject
{
    NSString *_bookName;
    NSMutableArray *_book;
}
@property(nonatomic,copy)NSString *bookName;
@property(nonatomic,strong)NSMutableArray *book;//强引用,数组中持有对象

-(id)initWithName:(NSString *)aBookName;
-(void)addCard:(AddressCard *)theCard;
-(int)entries;//统计
-(void)list;//显示
@end

AddressBook.m

#import "AddressBook.h"

@implementation AddressBook
@synthesize bookName = _bookName,book = _book;

-(id)initWithName:(NSString *)aBookName
{
    self = [super init];
    if (self)
    {
        _bookName = [NSString stringWithString:aBookName];
        _book = [NSMutableArray array];//空数组
    }
    return self;
}

-(id)init
{
    return [self initWithName:@"noName"];
}
-(void)addCard:(AddressCard *)theCard
{
    [_book addObject: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(@"==============================================");
}
@end

AddressBook+Option.h

#import "AddressBook.h"

@interface AddressBook (Option)
-(AddressCard *)lookup:(NSString *)theName;
-(void)removeCard:(AddressCard *)theCard;
-(void)sort;
@end

AddressBook+Option.m

#import "AddressBook+Option.h"

@implementation AddressBook (Option)
-(AddressCard *)lookup:(NSString *)theName
{
    for (AddressCard *nextCard in _book)
    {
        if ([nextCard.name caseInsensitiveCompare:theName] == NSOrderedSame)//不分大小写比较,返回值为-1,0,1
        {
            return nextCard;
        }
    }
    return nil;
}

-(void)removeCard:(AddressCard *)theCard
{
    [_book removeObject:theCard];
    //[_book removeObjectIdenticalTo:theCard];//以对象地址判断两对象是否相等
}
#if 0
-(void)sort
{
    [_book sortUsingSelector:@selector(compareName:)];
}
#else
-(void)sort//效率同上一样,好处省去了到AddressCard类写compareName:比较方法
{
    [_book sortUsingComparator:^(id obj1,id obj2)
    {
        return [[obj1 name]compare:[obj2 name]];
    }];
}
#endif
@end

AppDelegate.m

NSString *aName = @"jobs";
    NSString *aEmail = @"jobs@163.com";
    NSString *bName = @"wozz";
    NSString *bEmail = @"wozz@sina.com";
    NSString *cName = @"bill";
    NSString *cEmail = @"bill@qq.com";
    
    AddressCard *card1 = [[AddressCard alloc]init];
    AddressCard *card2 = [[AddressCard alloc]init];
    AddressCard *card3 = [[AddressCard alloc]init];
    
    [card1 setName:aName andEmail:aEmail];
    [card2 setName:bName andEmail:bEmail];
    [card3 setName:cName andEmail:cEmail];
    
//    [card1 print];
//    [card2 print];
    
    AddressBook *myBook = [[AddressBook alloc]initWithName:@"YiDaZhang address book"];
    NSLog(@"at first book count:%i",[myBook entries]);

    [myBook addCard:card1];
    [myBook addCard:card2];
    [myBook addCard:card3];
    NSLog(@"entries in address book after creation:%i",[myBook entries]);
    
    [myBook list];
    //通过name查找
    AddressCard *myCard = [myBook lookup:@"JOBS"];
    if (myCard != nil)
    {
        [myCard print];
    }
    else
    {
        NSLog(@"not found");
    }
    //删除,注意AddressCard中重写了NSObject的isEqual方法的条件,removeObject:和containsObject和indexOfObject都依赖isEqual来决定两对象是否相等
    AddressCard *myCard2 = [[AddressCard alloc]init];
    myCard2.name = @"jobs";
    myCard2.eMail = @"jobs@sohu.com";
    [myBook addCard:myCard2];
    [myBook list];
    
    if ([myBook respondsToSelector:@selector(removeCard:)])//证明当前对象能响应类目添加的方法
    {
        [myBook removeCard:myCard2];
    }
    [myBook list];
    //按name排序
    [myBook sort];
    [myBook list];

 

posted @ 2014-01-31 17:36  forrHuen  阅读(371)  评论(0编辑  收藏  举报