@fiy-fish
2015-07-15T20:15:24.000000Z
字数 4086
阅读 1387
Objective-c
// main.m
// day05-02-选择器
//
// Created by Aaron on 15/7/7.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
//选择器
#import "Dog.h"
#if 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
//SEL 选择器类型
//选择器就是一个容器----专门用来存放消息
Dog *dog = [[Dog alloc] init];
//[dog bark];
//[dog isEqualToString:@""];
SEL bl = @selector(bark);
//SEL bl = @selector(isEqualToString:);
//对象去触发选择器里面存储的方法
//[dog performSelector:@selector(bark)];
//[dog barkByTimes:2];
//有一个参数的方法
[dog performSelector:@selector(barkByTimes:) withObject:@2];
//有两个参数的方法
[dog performSelector:@selector(barkByTimes:andNum2:) withObject:@2 withObject:@4];
MyNumber *num = [[MyNumber alloc] initWithNum1:@10 andNum2:@11 andNum3:@12];
[dog performSelector:@selector(barkByMyNumber:) withObject:num];
}
return 0;
}
#endif
//检测某个对象是否实现了某个方法
#import "Cat.h"
#if 0
int main()
{
Dog *dog = [[Dog alloc] init];
Cat *cat = [[Cat alloc] init];
NSArray *array = @[@1,@"2",dog,@1,cat];
for(id obj in array)
{
//检测对象是否能响应某个消息
if([obj respondsToSelector:@selector(bark)])
{
[obj bark];
}
}
}
#endif
//控制控件的事件响应
//不同的对象触发事件,有不同的响应
#import "NSMutableArray+MyArraySort.h"
int main()
{
NSMutableArray *array1 = [NSMutableArray array];
NSMutableArray *array2 = [NSMutableArray array];
for(int i = 0; i < 10; i++)
{
Dog *dog = [[Dog alloc] init];
[dog setAge:i];
[array1 addObject:dog];
Cat *cat = [[Cat alloc] init];
[cat setAge:i];
[array2 addObject:cat];
}
// [array1 sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
// [array2 sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
[array1 mySortUsingSelector:@selector(sortByAgeWithOtherDog:)];
[array2 mySortUsingSelector:@selector(sortByAgeWithOtherDog:)];
NSLog(@"%@\n%@",array1,array2);
}
#import <Foundation/Foundation.h>
@interface NSMutableArray (MyArraySort)
-(void)mySortUsingSelector:(SEL)sel;
@end
#import "NSMutableArray+MyArraySort.h"
@implementation NSMutableArray (MyArraySort)
-(void)mySortUsingSelector:(SEL)sel
{
for(int i = 0; i < [self count]-1; i++)
{
for(int j = i+1; j < [self count]; j++)
{
id obj1 = [self objectAtIndex:i];
id obj2 = [self objectAtIndex:j];
if([obj1 performSelector:sel withObject:obj2])
{
[self exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
}
@end
#import <Foundation/Foundation.h>
@interface Cat : NSObject
{
NSInteger _age;
}
-(BOOL)sortByAgeWithOtherDog:(Cat *)cat;
-(void)setAge:(NSInteger)age;
-(void)bark;
@end
#import "Cat.h"
@implementation Cat
-(void)bark
{
NSLog(@"喵喵喵...");
}
-(void)setAge:(NSInteger)age
{
_age = age;
}
-(NSInteger)age
{
return _age;
}
-(BOOL)sortByAgeWithOtherDog:(Cat *)cat
{
return _age < [cat age];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"cat : age--%ld",_age];
}
@end
#import <Foundation/Foundation.h>
#import "MyNumber.h"
@interface Dog : NSObject
{
NSInteger _age;
}
-(BOOL)sortByAgeWithOtherDog:(Dog *)dog;
-(void)setAge:(NSInteger)age;
-(void)bark;
-(void)barkByTimes:(NSNumber *)times;
-(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2;
-(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3;
-(void)barkByMyNumber:(MyNumber *)myNum;
@end
#import "Dog.h"
@implementation Dog
-(void)bark
{
NSLog(@"汪汪汪....");
}
-(void)barkByTimes:(NSNumber *)times
{
for(int i = 0; i < [times integerValue]; i++)
{
NSLog(@"汪汪汪...%d",i);
}
}
-(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2
{
NSLog(@"num1 = %@ num2 = %@",times,num2);
}
-(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3
{
NSLog(@"num1 = %@ num2=%@ num3=%@",times,num2,num3);
}
-(void)barkByMyNumber:(MyNumber *)myNum
{
[self barkByTimes:[myNum num1] andNum2:[myNum num2] andNum3:[myNum num3]];
}
-(void)setAge:(NSInteger)age
{
_age = age;
}
-(NSInteger)age
{
return _age;
}
-(BOOL)sortByAgeWithOtherDog:(Dog *)dog
{
return _age > [dog age];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"dog : age--%ld",_age];
}
@end
#import <Foundation/Foundation.h>
@interface MyNumber : NSObject
{
NSNumber *_num1;
NSNumber *_num2;
NSNumber *_num3;
}
-(instancetype)initWithNum1:(NSNumber *)num1 andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3;
-(NSNumber *)num1;
-(NSNumber *)num2;
-(NSNumber *)num3;
@end
#import "MyNumber.h"
@implementation MyNumber
-(instancetype)initWithNum1:(NSNumber *)num1 andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3
{
if(self = [super init])
{
_num1 = num1;
_num2 = num2;
_num3 = num3;
}
return self;
}
-(NSNumber *)num1
{
return _num1;
}
-(NSNumber *)num2
{
return _num2;
}
-(NSNumber *)num3
{
return _num3;
}
@end