您好,登錄后才能下訂單哦!
首先要?jiǎng)?chuàng)建 AddressContact 類,
#import <Foundation/Foundation.h> //---------AddressContact的.h文件 @interface AddressContact : NSObject { NSString *_name; NSString *_sex; NSString *_phone; NSString *_address; NSString *_groupName; NSInteger _age; } - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone; - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex address:(NSString *)address groupName:(NSString *)groupName age:(NSInteger)age; - (NSString *)groupName; - (NSString *)phone; - (NSString *)sex; - (NSString *)name; - (NSInteger)age; //輸出信息, - (void)displayContactInfo; //比較年齡 - (NSComparisonResult)compareAge:(AddressContact *)addressContact; //比較姓名 - (NSComparisonResult)compareName:(AddressContact *)addressContact; @end
----
#import "AddressContact.h" //------AddressContact的.m文件 @implementation AddressContact - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone { return [self initWithName:name phone:phone sex:nil address:nil groupName:nil age:0]; } - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone sex:(NSString *)sex address:(NSString *)address groupName:(NSString *)groupName age:(NSInteger)age { self = [super init]; if (self) { _name = name; _phone = phone; _sex = sex; _address = address; _groupName = groupName; _age = age; } return self; } - (NSString *)groupName { return _groupName; } - (NSString *)phone { return _phone; } - (NSString *)sex { return _sex; } - (NSString *)name { return _name; } - (NSInteger)age { return _age; } - (void)displayContactInfo { NSLog(@"%@,%ld",_name,_age); } //比較年齡 - (NSComparisonResult)compareAge:(AddressContact *)addressContact { NSInteger myAge = [self age]; NSInteger otherAge = [addressContact age]; if (myAge > otherAge) { return NSOrderedAscending; }else if(myAge < otherAge){ return NSOrderedDescending; }else{ return NSOrderedSame; } } //比較姓名 - (NSComparisonResult)compareName:(AddressContact *)addressContact{ NSString *myName = [self name]; NSString *otherName = [addressContact name]; NSComparisonResult result = [myName compare:otherName]; if (result == NSOrderedAscending) { return NSOrderedDescending; }else if(result == NSOrderedDescending){ return NSOrderedAscending; }else { return NSOrderedSame; } } @end
-----------
然后在創(chuàng)建ContactManager類。。。。。。。。。。。
#import <Foundation/Foundation.h> // ------ContactManager的.h文件 #import "AddressContact.h" @interface ContactManager : NSObject { NSMutableDictionary *_contactDict; } //添加聯(lián)系人 - (void)addVContact:(AddressContact *)contact; //獲取某個(gè)分組下的聯(lián)系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName; //根據(jù)電話號(hào)碼搜索聯(lián)系人 - (AddressContact *)contactForPhoneNumber:(NSString *)phoneNumer; //獲取所有女性聯(lián)系人,按照年齡降序 - (NSArray *)contactForSex:(NSString *)sex; //根據(jù)姓名刪除某個(gè)聯(lián)系人 - (void)removeContactForName:(NSString *)name; //刪除某個(gè)分組的全部聯(lián)系人 -(void)removeContactForGroupName:(NSString *)groupName; @end
------
#import "ContactManager.h" //--------ContactManager的.m文件 @implementation ContactManager //添加聯(lián)系人 - (void)addVContact:(AddressContact *)contact { NSString *groupName = [contact groupName]; NSMutableArray *array = [_contactDict objectForKey:groupName]; if (array == nil) { array = [NSMutableArray arrayWithCapacity:0]; [array addObject:contact]; [_contactDict setObject:array forKey:groupName]; }else{ [array addObject: contact]; } } //獲取某個(gè)分組下的聯(lián)系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName { NSMutableArray *array = [_contactDict objectForKey:groupName]; [array sortUsingSelector:@selector(compareName:)]; return array; } //根據(jù)電話號(hào)碼搜索聯(lián)系人 - (AddressContact *)contactForPhoneNumber:(NSString *)phoneNumer { for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact phone] isEqualToString:phoneNumer]) { return contact; } } } return nil; } //獲取所有女性聯(lián)系人,按照年齡降序 - (NSArray *)contactForSex:(NSString *)sex { NSMutableArray *sexArray = [NSMutableArray arrayWithCapacity:0]; for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact sex] isEqualToString:sex]) { [sexArray addObject:contact]; } } } [sexArray sortedArrayUsingSelector:@selector(compareAge:)]; return sexArray; } //根據(jù)姓名刪除某個(gè)聯(lián)系人 - (void)removeContactForName:(NSString *)name { for (NSString *key in _contactDict) { NSMutableArray *array = [_contactDict objectForKey:key]; for (AddressContact *contact in array) { if ([[contact name] isEqualToString:name]) { [array removeObject:contact]; } } } } //刪除某個(gè)分組的全部聯(lián)系人 -(void)removeContactForGroupName:(NSString *)groupName { [_contactDict removeObjectForKey:groupName]; } @end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。