標籤:
今天我們學習兩個方法, 一個是對字串進行排序的方法,一個是對常量數進行排序的方法.
建立一個Person類,並且在Person.h檔案中,設定執行個體 初始化 以及setter getter方法 另外把排序的方法聲明在Person.h檔案中.
#import <Foundation/Foundation.h>
@interface Person : NSObject
//1.設定三個執行個體 name sex age
{
NSString *_name;
NSString *_sex;
NSInteger _age;
}
//2.指定初始化
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age;
//3.setter getter方法
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setSex:(NSString *)sex;
- (NSString *)sex;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
//4.聲明一個按姓名排序的方法
- (NSComparisonResult)compareByNameWithOtherPerson:(Person *)anotherPerson;
//5.聲明一個按年齡排序的方法
- (NSComparisonResult)compareByAgeWithOtherPerson:(Person*)anotherPerson;
//6.聲明一個description
- (NSString *)description;
@end
//調轉到Person.m中進行方法的實現
在Person.h檔案中聲明之後,將2.3.4.5.6複製下來,到Person.m檔案進行實現.
#import "Person.h"
@implementation Person
//2.指定初始化
- (id)initWithName:(NSString *)name
sex:(NSString *)sex
age:(NSInteger)age
{
self = [super init];
if (self) {
_name = name;
_sex = sex;
_age = age;
}
return self;
}
//3.setter getter方法
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setSex:(NSString *)sex
{
_sex = sex;
}
- (NSString *)sex
{
return _sex;
}
- (void)setAge:(NSInteger)age
{
_age = age;
}
- (NSInteger)age
{
return _age;
}
//4.聲明一個按姓名排序的方法
- (NSComparisonResult)compareByNameWithOtherPerson:(Person *)anotherPerson
{
if ([self.name compare:anotherPerson.name] > 0) {
return NSOrderedDescending;
}else if ([self.name compare:anotherPerson.name] < 0)
{
return NSOrderedAscending;
}
return NSOrderedSame;
}
//5.聲明一個按年齡排序的方法
- (NSComparisonResult)compareByAgeWithOtherPerson:(Person*)anotherPerson
{
if (self.age > anotherPerson.age) {
return NSOrderedDescending;
}else if (self.age < anotherPerson.age)
{
return NSOrderedAscending;
}
return NSOrderedSame;
}
//6.聲明一個description
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@ , sex: %@, age:%ld",_name,_sex,_age];
}
@end
方法聲明及實現之後,我們就要跳轉到main函數中進行調用方法
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//7.建立4個對象
Person *obj1 = [[Person alloc] initWithName:@"Jack" sex:@"male" age:22];
Person *obj2 = [[Person alloc] initWithName:@"Henry" sex:@"male" age:28];
Person *obj3 = [[Person alloc] initWithName:@"Elyse" sex:@"female" age:26];
Person *obj4 = [[Person alloc] initWithName:@"Lisa" sex:@"female" age:24];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:obj1,obj2,obj3,obj4, nil];
//8. 數組按照名字排序
[array sortUsingSelector:@selector(compareByNameWithOtherPerson:)];
//調用compareByName這個方法,在啟動並執行時候,會首先跳轉到Person.h中,看看有沒有這個方法,找到後,就會跳轉到Person.m中,運行方法,然後在跳轉到main函數中,把結果顯示出來.
NSLog(@"%@",array);
//9. 數組按照年齡排序
[array sortUsingSelector:@selector(compareByAgeWithOtherPerson:)];
//調用compareByAge這個方法,在啟動並執行時候,會首先跳轉到Person.h中,看看有沒有這個方法,找到後,就會跳轉到Person.m中,運行方法,然後在跳轉到main函數中,把結果顯示出來.
NSLog(@"%@",array);
}
return 0;
}
這次主要是把字串排序和常量數的排序掌握, 以後應該還會經常用到,所以把方法記住!
Objective-C sortUsingSelector方法