SEL 类成员方法的指针
可以理解 @selector()就是取类方法的编号,它的行为基本可以等同C语言的中函数指针,只不过C语言中,可以把函数名直接赋给一个函数指针,而Objective-C的类不能直接应用函数指针,这样只能做一个@selector()语法来取.
它的结果是一个SEL类型.这个类型本质是类方法的编号(函数地址).
下面给出两个例子,来简单实现selector的两个用法:
test1():模拟不同情境下,执行对象不同的方法;
test2():用选择器来实现对不同字段进行排序.
//
// main.m
// Selector
//
// Created by MacroHong on 9/22/13.
// Copyright © 2013 MacroHong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Student.h"
void test1(void); // 选择器相当于函数指针变量
void test2(void); // 利用选择器给不同字段排序
int main(int argc, const char * argv[]) {
@autoreleasepool {
// test1(); // 选择器相当于函数指针变量
test2(); // 利用选择器给不同字段排序
}
return 0;
}
void test1(void) {
Dog *dog = [[Dog alloc] init];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//定义并给选择器赋值
SEL sel = @selector(run);
// 利用选择器运行相应的方法
[dog performSelector:sel]; // 相当于运行 [dog run];
sel = @selector(jump);
[dog performSelector:sel];
sel = @selector(bark);
[dog performSelector:sel];
#pragma clang diagnostic pop // ARC中加此三句可以消去内存泄露的警告
}
void test2(void) {
NSArray *names = @[@"Tim", @"Bob", @"Alice", @"Ferry", @"Kitty"];
NSMutableArray *stus = [[NSMutableArray alloc] init];
for (NSString *name in names) {
Student *stu = [[Student alloc] init];
stu.name = name;
stu.age = arc4random() % 90;
stu.score = arc4random() % 100;
[stus addObject: stu];
}
// [students sortUsingSelector:@selector(olderThanStudent:)];//年龄排序
// [students sortUsingSelector:@selector(nameAfterStudent:)];//姓名排序
[stus sortUsingSelector:@selector(scoreLowerThanStudent:)];//成绩排序
for (Student * stu in stus) {
NSLog(@"name:%@ age:%ld score:%ld", stu.name, stu.age, stu.score);
}
}
//
// Dog.h
// Selector
//
// Created by MacroHong on 9/22/13.
// Copyright © 2013 MacroHong. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Dog : NSObject
-(void)bark;
-(void)run;
-(void)jump;
@end
//
// Dog.m
// Selector
//
// Created by MacroHong on 9/22/13.
// Copyright © 2013 MacroHong. All rights reserved.
//
#import "Dog.h"
@implementation Dog
-(void)bark {
NSLog(@"Dog Won~ Won~");
}
-(void)run {
NSLog(@"Dog run...");
}
-(void)jump {
NSLog(@"Dog jump...");
}
@end
//
// Student.h
// Selector
//
// Created by MacroHong on 9/22/13.
// Copyright © 2013 MacroHong. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (copy) NSString * name;
@property NSInteger age;
@property NSInteger score;
/**
年龄排序
*/
-(BOOL) olderThanStudent:(Student *)anotherStudent;
/**
成绩排序
*/
-(BOOL) scoreLowerThanStudent:(Student *)anotherStudent;
/**
姓名排序
*/
-(BOOL) nameAfterStudent:(Student *)anotherStudent;
@end
//
// Student.m
// Selector
//
// Created by MacroHong on 9/22/13.
// Copyright © 2013 MacroHong. All rights reserved.
//
#import "Student.h"
@implementation Student
-(BOOL) olderThanStudent:(Student *)anotherStudent {
return self.age > anotherStudent.age;
}
-(BOOL) scoreLowerThanStudent:(Student *)anotherStudent {
return self.score < anotherStudent.score;
}
-(BOOL) nameAfterStudent:(Student *)anotherStudent {
return ([self.name compare:anotherStudent.name] == NSOrderedDescending);
}
@end