码迷,mamicode.com
首页 > 其他好文 > 详细

OC-通讯录

时间:2015-08-04 00:42:57      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:objective-c

//
//  Card.h
//  通讯录
//
//  Created by 邱学伟 on 15/8/3.
//  Copyright (c) 2015年 邱学伟. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Card : NSObject

@property (nonatomic, copy) NSString *contactName;
@property (nonatomic, assign) NSUInteger phoneNum;

+(instancetype)cardWithContanctName:(NSString *)newName andPhoneNum:(NSUInteger)newPoneNum;

@end
//
//  Card.m
//  通讯录
//
//  Created by 邱学伟 on 15/8/3.
//  Copyright (c) 2015年 邱学伟. All rights reserved.
//

#import "Card.h"

@implementation Card
@synthesize contactName,phoneNum;
+(instancetype)cardWithContanctName:(NSString *)newName andPhoneNum:(NSUInteger)newPoneNum{
    Card *card = [[Card alloc]init];
    card.contactName=newName;
    card.phoneNum=newPoneNum;
    return card;
}
@end
//
//  PhoneBook.h
//  通讯录
//
//  Created by 邱学伟 on 15/8/3.
//  Copyright (c) 2015年 邱学伟. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Card.h"
@interface PhoneBook : NSObject
@property (nonatomic, copy)NSString *masterName;
@property (nonatomic, strong)NSMutableArray *cards;

//展示当前电话本中所有联系人信息
-(void)showInfo;

//类方法的初始化
+(instancetype)phoneBookWithMasterName:(NSString *)name andCards:(NSArray *)cardAry;
//添加名片
-(void)addNewCard:(Card *)card;

//删除名片
-(void)deleteCard:(Card *)card;
-(void)deleteCardWithName:(NSString *)cardName;
-(void)deleteCardWithPoneNum:(NSUInteger)phoneNum;

//根据电话号码排序
-(void)sortByPhoneNumAsc;

//根据姓名排序
-(void)sortByNameAsc;



@end
//
//  PhoneBook.m
//  通讯录
//
//  Created by 邱学伟 on 15/8/3.
//  Copyright (c) 2015年 邱学伟. All rights reserved.
//

#import "PhoneBook.h"
#import "Card.h"
@implementation PhoneBook

@synthesize masterName,cards;

//类方法的初始化
#pragma mark 类方法得到对象
+(instancetype)phoneBookWithMasterName:(NSString *)name andCards:(NSArray *)cardAry{
    PhoneBook *book = [PhoneBook new];
    book.masterName=name;
    book.cards=[NSMutableArray arrayWithObject:cardAry];
    return book;
}

#pragma mark 展示信息
//展示当前电话本中所有联系人信息
-(void)showInfo{
    NSLog(@"show %@,",masterName);
    for (Card *card in cards) {
        NSLog(@"联系人信息:%@, 电话 %lu",card.contactName,card.phoneNum);
    }
}

//添加名片
#pragma mark 添加名片
-(void)addNewCard:(Card *)card{
    [cards addObject:card];
}

//删除名片
-(void)deleteCard:(Card *)card{
    [cards removeObject:card];
}

-(void)deleteCardWithName:(NSString *)cardName{
    for (int i=0; i<cards.count; ) {
        Card *card = [cards objectAtIndex:i];
        if ([card.contactName isEqualToString:cardName]) {
            [cards removeObject:card];
        }else{
            i++;
        }
    }
}
-(void)deleteCardWithPoneNum:(NSUInteger)phoneNum{
    for (int i=0; i<cards.count; ) {
        Card *card = [cards objectAtIndex:i];
        if (card.phoneNum==phoneNum) {
            [cards removeObject:card];
        }else{
            i++;
        }

    }
}

//根据电话号码排序
#pragma mark 根据号码排序
-(void)sortByPhoneNumAsc{
    for (int i=0; i<cards.count-1; i++) {
        for (int j=0; j<cards.count-1-i; j++) {
            if (((Card *)cards[j]).phoneNum > ((Card *)cards[j+1]).phoneNum) {
                [cards exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }
}

//根据姓名排序
-(void)sortByNameAsc{
    for (int i=0; i<cards.count-1; i++) {
        for (int j=0; j<cards.count-1-i; j++) {
            if ([((Card *)cards[j]).contactName compare:((Card *)cards[j+1]).contactName] == NSOrderedDescending) {
                [cards exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }
}


@end
//
//  main.m
//  通讯录
//
//  Created by 邱学伟 on 15/8/3.
//  Copyright (c) 2015年 邱学伟. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Card.h"
#import "PhoneBook.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       //名片1
        Card *card1 = [Card new];
        card1.contactName = @"sunny";
        card1.phoneNum = 18363858590;

        //名片2
        Card *card2 = [Card cardWithContanctName:@"apple" andPhoneNum:18363858591];

        //名片3
        Card *card3 = [Card cardWithContanctName:@"ios" andPhoneNum:18363858592];

        //名片4
        Card *card4 = [Card cardWithContanctName:@"oc" andPhoneNum:18363858593];

        //名片5
        Card *card5 = [Card cardWithContanctName:@"ui" andPhoneNum:18363858594];

        //电话本1
        PhoneBook *phoneBook1 = [PhoneBook new];
        phoneBook1.masterName = @"lovke";
        phoneBook1.cards = [NSMutableArray array];//必须得初始化数组
        [phoneBook1.cards addObjectsFromArray:@[card1,card2]];

        //展示
        [phoneBook1 showInfo];

        //电话本2
        PhoneBook *phoneBook2 = [PhoneBook phoneBookWithMasterName:@"kehui" andCards:(NSArray *)card2];

        [phoneBook2 addNewCard:card1];
        [phoneBook2 addNewCard:card3];
        [phoneBook2 addNewCard:card4];
        [phoneBook2 addNewCard:card5];
        //展示
        [phoneBook2 showInfo];

//        //删除
//        [phoneBook2 deleteCard:card1];
//        //展示
//        [phoneBook2 showInfo];
//        
//        //按姓名删除
//        [phoneBook2 deleteCardWithName:@"ui"];
//        //展示
//        [phoneBook2 showInfo];
//        
//        //按电话号码删除
//        [phoneBook2 deleteCardWithPoneNum:18363858593];
//        //展示
//        [phoneBook2 showInfo];

        //电话本3
        PhoneBook *phoneBook3 = [PhoneBook phoneBookWithMasterName:@"qiuxuewei" andCards:(NSArray *)card1];

        [phoneBook3 addNewCard:card2];
        [phoneBook3 addNewCard:card3];
        [phoneBook3 addNewCard:card5];
        [phoneBook3 addNewCard:card4];
        [phoneBook3 showInfo];

        //电话排序
        [phoneBook3 sortByPhoneNumAsc];
        [phoneBook3 showInfo];

        //姓名排序
        [phoneBook3 sortByNameAsc];
        [phoneBook3 showInfo];
    }
    return 0;
}

运行结果:
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

OC-通讯录

标签:objective-c

原文地址:http://blog.csdn.net/qxuewei/article/details/47265821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!