-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huangyibiao
authored and
huangyibiao
committed
Jan 12, 2016
1 parent
b94807f
commit df83dce
Showing
15 changed files
with
592 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+9.5 KB
(130%)
RuntimeDemo.xcworkspace/xcuserdata/huangyibiao.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// HYBMethodExchange.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 16/1/4. | ||
// Copyright © 2016年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBMethodExchange : NSObject | ||
|
||
+ (void)test; | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
// | ||
// HYBMethodExchange.m | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 16/1/4. | ||
// Copyright © 2016年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import "HYBMethodExchange.h" | ||
#import <objc/runtime.h> | ||
#import "NSMutableArray+Swizzling.h" | ||
#import "NSArray+Swizzling.h" | ||
|
||
@implementation HYBMethodExchange | ||
|
||
|
||
+ (void)test { | ||
|
||
// Method originalMethod = class_getInstanceMethod([NSArray class], @selector(lastObject)); | ||
// Method newMedthod = class_getInstanceMethod([NSArray class], NSSelectorFromString(@"hdf_lastObject")); | ||
// method_exchangeImplementations(originalMethod, newMedthod); | ||
|
||
NSMutableArray *array = [@[@"value", @"value1"] mutableCopy]; | ||
[array lastObject]; | ||
|
||
[array removeObject:@"value"]; | ||
[array removeObject:nil]; | ||
[array addObject:@"12"]; | ||
[array addObject:nil]; | ||
[array insertObject:nil atIndex:0]; | ||
[array insertObject:@"sdf" atIndex:10]; | ||
[array objectAtIndex:100]; | ||
[array removeObjectAtIndex:10]; | ||
|
||
NSMutableArray *anotherArray = [[NSMutableArray alloc] init]; | ||
[anotherArray objectAtIndex:0]; | ||
|
||
NSString *nilStr = nil; | ||
NSArray *array1 = @[@"ara", @"sdf", @"dsfdsf", nilStr]; | ||
NSLog(@"array1.count = %lu", array1.count); | ||
} | ||
|
||
// C语言版 | ||
void swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) { | ||
Method originalMethod = class_getInstanceMethod(class, originalSelector); | ||
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | ||
|
||
// 若已经存在,则添加会失败 | ||
BOOL didAddMethod = class_addMethod(class, | ||
originalSelector, | ||
method_getImplementation(swizzledMethod), | ||
method_getTypeEncoding(swizzledMethod)); | ||
|
||
// 若原来的方法并不存在,则添加即可 | ||
if (didAddMethod) { | ||
class_replaceMethod(class, | ||
swizzledSelector, | ||
method_getImplementation(originalMethod), | ||
method_getTypeEncoding(originalMethod)); | ||
} else { | ||
method_exchangeImplementations(originalMethod, swizzledMethod); | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// HYBMethodLearn.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 16/1/12. | ||
// Copyright © 2016年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBMethodLearn : NSObject | ||
|
||
+ (void)test; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// HYBMethodLearn.m | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 16/1/12. | ||
// Copyright © 2016年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import "HYBMethodLearn.h" | ||
#import <objc/runtime.h> | ||
#import <objc/message.h> | ||
|
||
@implementation HYBMethodLearn | ||
|
||
- (int)testInstanceMethod:(NSString *)name andValue:(NSNumber *)value { | ||
NSLog(@"%@", name); | ||
|
||
return value.intValue; | ||
} | ||
|
||
- (NSArray *)arrayWithNames:(NSArray *)names { | ||
NSLog(@"%@", names); | ||
return names; | ||
} | ||
|
||
- (void)getMethods { | ||
unsigned int outCount = 0; | ||
Method *methodList = class_copyMethodList(self.class, &outCount); | ||
|
||
for (unsigned int i = 0; i < outCount; ++i) { | ||
Method method = methodList[i]; | ||
|
||
SEL methodName = method_getName(method); | ||
NSLog(@"方法名:%@", NSStringFromSelector(methodName)); | ||
|
||
// 获取方法的参数类型 | ||
unsigned int argumentsCount = method_getNumberOfArguments(method); | ||
char argName[512] = {}; | ||
for (unsigned int j = 0; j < argumentsCount; ++j) { | ||
method_getArgumentType(method, j, argName, 512); | ||
|
||
NSLog(@"第%u个参数类型为:%s", j, argName); | ||
memset(argName, '\0', strlen(argName)); | ||
} | ||
|
||
char returnType[512] = {}; | ||
method_getReturnType(method, returnType, 512); | ||
NSLog(@"返回值类型:%s", returnType); | ||
|
||
// type encoding | ||
NSLog(@"TypeEncoding: %s", method_getTypeEncoding(method)); | ||
} | ||
|
||
free(methodList); | ||
} | ||
|
||
|
||
+ (void)test { | ||
HYBMethodLearn *m = [[HYBMethodLearn alloc] init]; | ||
// [m getMethods]; | ||
|
||
((void (*)(id, SEL))objc_msgSend)((id)m, @selector(getMethods)); | ||
|
||
// 这就是为什么有四个参数的原因 | ||
int returnValue = ((int (*)(id, SEL, NSString *, NSNumber *)) | ||
objc_msgSend)((id)m, | ||
@selector(testInstanceMethod:andValue:), | ||
@"标哥的技术博客", | ||
@100); | ||
NSLog(@"return value is %d", returnValue); | ||
|
||
// 获取方法 | ||
Method method = class_getInstanceMethod([self class], @selector(testInstanceMethod:andValue:)); | ||
|
||
// 调用函数 | ||
returnValue = ((int (*)(id, Method, NSString *, NSNumber *))method_invoke)((id)m, method, @"测试使用method_invoke", @11); | ||
NSLog(@"call return vlaue is %d", returnValue); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// HYBPropertyLearn.h | ||
// RuntimeDemo | ||
// | ||
// Created by huangyibiao on 16/1/10. | ||
// Copyright © 2016年 huangyibiao. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface HYBPropertyLearn : NSObject { | ||
float _websiteTitle; | ||
|
||
@private | ||
float _privateAttribute; | ||
} | ||
|
||
@property (nonatomic, copy) NSString *title; | ||
@property (nonatomic, strong) NSArray *names; | ||
@property (nonatomic, assign) int count; | ||
@property (nonatomic, weak) id delegate; | ||
@property (atomic, strong) NSNumber *atomicProperty; | ||
|
||
+ (void)test; | ||
|
||
@end |
Oops, something went wrong.