您好,登錄后才能下訂單哦!
首先要創建 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
-----------
然后在創建ContactManager類。。。。。。。。。。。
#import <Foundation/Foundation.h> // ------ContactManager的.h文件 #import "AddressContact.h" @interface ContactManager : NSObject { NSMutableDictionary *_contactDict; } //添加聯系人 - (void)addVContact:(AddressContact *)contact; //獲取某個分組下的聯系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName; //根據電話號碼搜索聯系人 - (AddressContact *)contactForPhoneNumber:(NSString *)phoneNumer; //獲取所有女性聯系人,按照年齡降序 - (NSArray *)contactForSex:(NSString *)sex; //根據姓名刪除某個聯系人 - (void)removeContactForName:(NSString *)name; //刪除某個分組的全部聯系人 -(void)removeContactForGroupName:(NSString *)groupName; @end
------
#import "ContactManager.h" //--------ContactManager的.m文件 @implementation ContactManager //添加聯系人 - (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]; } } //獲取某個分組下的聯系人。 - (NSArray *)allContactsForGroupName:(NSString *)groupName { NSMutableArray *array = [_contactDict objectForKey:groupName]; [array sortUsingSelector:@selector(compareName:)]; return array; } //根據電話號碼搜索聯系人 - (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; } //獲取所有女性聯系人,按照年齡降序 - (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; } //根據姓名刪除某個聯系人 - (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]; } } } } //刪除某個分組的全部聯系人 -(void)removeContactForGroupName:(NSString *)groupName { [_contactDict removeObjectForKey:groupName]; } @end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。