Objectvie-C NSMutableArray的用法(通讯录实例)

NSMutableArray是可变数组,以下的例子是一个通讯录,可增加、删除、搜索联系人。
AddressCard.h:


#import 

@interface AddressCard : NSObject
{
    NSString * nickname;
    //存放昵称
    NSString * email;
    //电邮
    
}
@property (copy,nonatomic) NSString * nickname,* email;
//属性,必须加copy nonatomic参数
-(void)setNickName:(NSString *)n andEmail:(NSString *) e;
//设置昵称和邮件的接收器
-(NSComparisonResult) compareNames:(AddressCard *) card;
//排序用,返回nickname对比的结果
@end

AddressCard.m:


#import "AddressCard.h"

@implementation AddressCard
-(void)setNickName:(NSString *)n andEmail:(NSString *)e
{
    self.nickname=[NSString stringWithString:n];
    self.email=[NSString stringWithString:e];
    
}
- (NSString *)description
{
    return [nickname stringByAppendingFormat:@"     Email: %@",email];
}

-(BOOL) isEqual:(AddressCard *)object
{
    if([nickname isEqualToString:[object nickname]]&&[email isEqualToString:[object email]])
    {
        return YES;
    }
    return NO;
}
-(NSComparisonResult) compareNames:(AddressCard *)card
{
    return [nickname compare:[card nickname]];
}

@synthesize nickname,email;
@end

AddressBook.h:


#import 
#import "AddressCard.h"

@interface AddressBook : NSObject
{
    NSString *bookName;
    NSMutableArray *book;
}
-(id)initWithName:(NSString *) name;
-(void)addCard:(AddressCard *) card;
//添加联系人
-(int)entries;
//返回联系人数量
-(void)list;
//列出所有联系人
-(void)deleteCard:(AddressCard *)card;
//删除联系人
-(void)sort;
//排序

-(AddressCard *) lookup:(NSString *) theName;
//搜索联系人

@end

AddressBook.m:


#import "AddressBook.h"

@implementation AddressBook
-(id)initWithName:(NSString *)name
{
    self=[super init];
    name=[NSString stringWithString:name];
    book=[NSMutableArray new];
    return self;
}
-(void)addCard:(AddressCard *)card
{
    [book addObject:card];
}
-(int)entries
{
    return [book count];
}
-(void)list
{
    for(AddressCard *card in book)
    {
        NSLog(@"%@",card);
    }
}
-(AddressCard *) lookup:(NSString *)theName
{
    for(AddressCard * nextCard in book)
    {
        if([theName caseInsensitiveCompare:[nextCard nickname]]==NSOrderedSame)
        {
            //忽略大小写,查找联系人
            return nextCard;
        }
    }
    return nil;
}
-(void)deleteCard:(AddressCard *)card
{
    [book removeObject:card];
}

-(void)sort
{
    [book sortUsingSelector:@selector(compareNames:)];
    //使用选择器排序,使用AddressCard中的compareNames方法作选择器
}

@end

main.m:


#import 
#import "AddressBook.h"
//AddressBook.h中已经有导入AddressCard.h,所以这里无需再导

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        AddressCard *card1=[AddressCard new];
        [card1 setNickName:@"bcd" andEmail:@"adf@adf.com"];
        AddressBook *book=[AddressBook alloc];
        book=[book initWithName:@"MyAddressBook"];
        [book addCard:card1];
        AddressCard *card2=[AddressCard new];
        [card2 setNickName:@"afd" andEmail:@"asdf@dadfsff.com"];
        [book addCard:card2];
		
        [book list];
		//列出排序前所有的AddressCard
		[book sort];
		//排序
		[book list];
		//列出排序后所有的AddressCard


		
        AddressCard *findCard=[book lookup:@"bcd"];
        NSLog(@"%@",[findCard email]);
		//搜索
        [book deleteCard:findCard];
		//删除
        [book list];
		
        
    }
    return 0;
    
}

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记