[关闭]
@fiy-fish 2015-07-15T20:15:24.000000Z 字数 4086 阅读 1412

day05-02-选择器

Objective-c


  1. // main.m
  2. // day05-02-选择器
  3. //
  4. // Created by Aaron on 15/7/7.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. //选择器
  9. #import "Dog.h"
  10. #if 0
  11. int main(int argc, const char * argv[]) {
  12. @autoreleasepool {
  13. //SEL 选择器类型
  14. //选择器就是一个容器----专门用来存放消息
  15. Dog *dog = [[Dog alloc] init];
  16. //[dog bark];
  17. //[dog isEqualToString:@""];
  18. SEL bl = @selector(bark);
  19. //SEL bl = @selector(isEqualToString:);
  20. //对象去触发选择器里面存储的方法
  21. //[dog performSelector:@selector(bark)];
  22. //[dog barkByTimes:2];
  23. //有一个参数的方法
  24. [dog performSelector:@selector(barkByTimes:) withObject:@2];
  25. //有两个参数的方法
  26. [dog performSelector:@selector(barkByTimes:andNum2:) withObject:@2 withObject:@4];
  27. MyNumber *num = [[MyNumber alloc] initWithNum1:@10 andNum2:@11 andNum3:@12];
  28. [dog performSelector:@selector(barkByMyNumber:) withObject:num];
  29. }
  30. return 0;
  31. }
  32. #endif
  33. //检测某个对象是否实现了某个方法
  34. #import "Cat.h"
  35. #if 0
  36. int main()
  37. {
  38. Dog *dog = [[Dog alloc] init];
  39. Cat *cat = [[Cat alloc] init];
  40. NSArray *array = @[@1,@"2",dog,@1,cat];
  41. for(id obj in array)
  42. {
  43. //检测对象是否能响应某个消息
  44. if([obj respondsToSelector:@selector(bark)])
  45. {
  46. [obj bark];
  47. }
  48. }
  49. }
  50. #endif
  51. //控制控件的事件响应
  52. //不同的对象触发事件,有不同的响应
  53. #import "NSMutableArray+MyArraySort.h"
  54. int main()
  55. {
  56. NSMutableArray *array1 = [NSMutableArray array];
  57. NSMutableArray *array2 = [NSMutableArray array];
  58. for(int i = 0; i < 10; i++)
  59. {
  60. Dog *dog = [[Dog alloc] init];
  61. [dog setAge:i];
  62. [array1 addObject:dog];
  63. Cat *cat = [[Cat alloc] init];
  64. [cat setAge:i];
  65. [array2 addObject:cat];
  66. }
  67. // [array1 sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
  68. // [array2 sortUsingSelector:@selector(sortByAgeWithOtherDog:)];
  69. [array1 mySortUsingSelector:@selector(sortByAgeWithOtherDog:)];
  70. [array2 mySortUsingSelector:@selector(sortByAgeWithOtherDog:)];
  71. NSLog(@"%@\n%@",array1,array2);
  72. }

  1. #import <Foundation/Foundation.h>
  2. @interface NSMutableArray (MyArraySort)
  3. -(void)mySortUsingSelector:(SEL)sel;
  4. @end

  1. #import "NSMutableArray+MyArraySort.h"
  2. @implementation NSMutableArray (MyArraySort)
  3. -(void)mySortUsingSelector:(SEL)sel
  4. {
  5. for(int i = 0; i < [self count]-1; i++)
  6. {
  7. for(int j = i+1; j < [self count]; j++)
  8. {
  9. id obj1 = [self objectAtIndex:i];
  10. id obj2 = [self objectAtIndex:j];
  11. if([obj1 performSelector:sel withObject:obj2])
  12. {
  13. [self exchangeObjectAtIndex:i withObjectAtIndex:j];
  14. }
  15. }
  16. }
  17. }
  18. @end

  1. #import <Foundation/Foundation.h>
  2. @interface Cat : NSObject
  3. {
  4. NSInteger _age;
  5. }
  6. -(BOOL)sortByAgeWithOtherDog:(Cat *)cat;
  7. -(void)setAge:(NSInteger)age;
  8. -(void)bark;
  9. @end

  1. #import "Cat.h"
  2. @implementation Cat
  3. -(void)bark
  4. {
  5. NSLog(@"喵喵喵...");
  6. }
  7. -(void)setAge:(NSInteger)age
  8. {
  9. _age = age;
  10. }
  11. -(NSInteger)age
  12. {
  13. return _age;
  14. }
  15. -(BOOL)sortByAgeWithOtherDog:(Cat *)cat
  16. {
  17. return _age < [cat age];
  18. }
  19. -(NSString *)description
  20. {
  21. return [NSString stringWithFormat:@"cat : age--%ld",_age];
  22. }
  23. @end

  1. #import <Foundation/Foundation.h>
  2. #import "MyNumber.h"
  3. @interface Dog : NSObject
  4. {
  5. NSInteger _age;
  6. }
  7. -(BOOL)sortByAgeWithOtherDog:(Dog *)dog;
  8. -(void)setAge:(NSInteger)age;
  9. -(void)bark;
  10. -(void)barkByTimes:(NSNumber *)times;
  11. -(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2;
  12. -(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3;
  13. -(void)barkByMyNumber:(MyNumber *)myNum;
  14. @end

  1. #import "Dog.h"
  2. @implementation Dog
  3. -(void)bark
  4. {
  5. NSLog(@"汪汪汪....");
  6. }
  7. -(void)barkByTimes:(NSNumber *)times
  8. {
  9. for(int i = 0; i < [times integerValue]; i++)
  10. {
  11. NSLog(@"汪汪汪...%d",i);
  12. }
  13. }
  14. -(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2
  15. {
  16. NSLog(@"num1 = %@ num2 = %@",times,num2);
  17. }
  18. -(void)barkByTimes:(NSNumber *)times andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3
  19. {
  20. NSLog(@"num1 = %@ num2=%@ num3=%@",times,num2,num3);
  21. }
  22. -(void)barkByMyNumber:(MyNumber *)myNum
  23. {
  24. [self barkByTimes:[myNum num1] andNum2:[myNum num2] andNum3:[myNum num3]];
  25. }
  26. -(void)setAge:(NSInteger)age
  27. {
  28. _age = age;
  29. }
  30. -(NSInteger)age
  31. {
  32. return _age;
  33. }
  34. -(BOOL)sortByAgeWithOtherDog:(Dog *)dog
  35. {
  36. return _age > [dog age];
  37. }
  38. -(NSString *)description
  39. {
  40. return [NSString stringWithFormat:@"dog : age--%ld",_age];
  41. }
  42. @end

  1. #import <Foundation/Foundation.h>
  2. @interface MyNumber : NSObject
  3. {
  4. NSNumber *_num1;
  5. NSNumber *_num2;
  6. NSNumber *_num3;
  7. }
  8. -(instancetype)initWithNum1:(NSNumber *)num1 andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3;
  9. -(NSNumber *)num1;
  10. -(NSNumber *)num2;
  11. -(NSNumber *)num3;
  12. @end

  1. #import "MyNumber.h"
  2. @implementation MyNumber
  3. -(instancetype)initWithNum1:(NSNumber *)num1 andNum2:(NSNumber *)num2 andNum3:(NSNumber *)num3
  4. {
  5. if(self = [super init])
  6. {
  7. _num1 = num1;
  8. _num2 = num2;
  9. _num3 = num3;
  10. }
  11. return self;
  12. }
  13. -(NSNumber *)num1
  14. {
  15. return _num1;
  16. }
  17. -(NSNumber *)num2
  18. {
  19. return _num2;
  20. }
  21. -(NSNumber *)num3
  22. {
  23. return _num3;
  24. }
  25. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注