├── README.md ├── runtime知识点.xcodeproj ├── xcuserdata │ └── songqingbo.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ └── xcschememanagement.plist ├── project.xcworkspace │ └── contents.xcworkspacedata └── project.pbxproj ├── runtime知识点 ├── UIViewController+Traking.h ├── UIView+SQBView.h ├── ViewController.h ├── AppDelegate.h ├── main.m ├── MyObject.h ├── UIViewController+Traking.m ├── MyObject.m ├── Info.plist ├── Base.lproj │ ├── Main.storyboard │ └── LaunchScreen.storyboard ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── UIView+SQBView.m ├── AppDelegate.m └── ViewController.m ├── runtime知识点Tests ├── Info.plist └── runtime___Tests.m └── runtime知识点UITests ├── Info.plist └── runtime___UITests.m /README.md: -------------------------------------------------------------------------------- 1 | # runtime 2 | 基本介绍了runtime中的大部分功能,包括对类与对象、成员变量与属性、方法与消息 3 | -------------------------------------------------------------------------------- /runtime知识点.xcodeproj/xcuserdata/songqingbo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /runtime知识点.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /runtime知识点/UIViewController+Traking.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+Traking.h 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/24. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIViewController (Traking) 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /runtime知识点/UIView+SQBView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+SQBView.h 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/23. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIView (SQBView) 12 | - (void)setTapActionWithBlock:(void (^)(void))block; 13 | @end 14 | -------------------------------------------------------------------------------- /runtime知识点/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | @property (nonatomic,strong) NSString *person; 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /runtime知识点/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /runtime知识点/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /runtime知识点/MyObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // MyObject.h 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MyObject : NSObject 12 | @property(strong,nonatomic) NSString *string; 13 | @property(strong,nonatomic) NSArray *array; 14 | 15 | - (void)method1; 16 | - (void)method2; 17 | - (void)classMethod1; 18 | @end 19 | -------------------------------------------------------------------------------- /runtime知识点.xcodeproj/xcuserdata/songqingbo.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | runtime知识点.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /runtime知识点Tests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /runtime知识点UITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /runtime知识点Tests/runtime___Tests.m: -------------------------------------------------------------------------------- 1 | //___FILEHEADER___ 2 | 3 | #import 4 | 5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase 6 | 7 | @end 8 | 9 | @implementation ___FILEBASENAMEASIDENTIFIER___ 10 | 11 | - (void)setUp { 12 | [super setUp]; 13 | // Put setup code here. This method is called before the invocation of each test method in the class. 14 | } 15 | 16 | - (void)tearDown { 17 | // Put teardown code here. This method is called after the invocation of each test method in the class. 18 | [super tearDown]; 19 | } 20 | 21 | - (void)testExample { 22 | // This is an example of a functional test case. 23 | // Use XCTAssert and related functions to verify your tests produce the correct results. 24 | } 25 | 26 | - (void)testPerformanceExample { 27 | // This is an example of a performance test case. 28 | [self measureBlock:^{ 29 | // Put the code you want to measure the time of here. 30 | }]; 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /runtime知识点UITests/runtime___UITests.m: -------------------------------------------------------------------------------- 1 | //___FILEHEADER___ 2 | 3 | #import 4 | 5 | @interface ___FILEBASENAMEASIDENTIFIER___ : XCTestCase 6 | 7 | @end 8 | 9 | @implementation ___FILEBASENAMEASIDENTIFIER___ 10 | 11 | - (void)setUp { 12 | [super setUp]; 13 | 14 | // Put setup code here. This method is called before the invocation of each test method in the class. 15 | 16 | // In UI tests it is usually best to stop immediately when a failure occurs. 17 | self.continueAfterFailure = NO; 18 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 19 | [[[XCUIApplication alloc] init] launch]; 20 | 21 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 22 | } 23 | 24 | - (void)tearDown { 25 | // Put teardown code here. This method is called after the invocation of each test method in the class. 26 | [super tearDown]; 27 | } 28 | 29 | - (void)testExample { 30 | // Use recording to get started writing UI tests. 31 | // Use XCTAssert and related functions to verify your tests produce the correct results. 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /runtime知识点/UIViewController+Traking.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | // 7 | // UIViewController+Traking.m 8 | // runtime知识点 9 | // 10 | // Created by songqingbo on 2017/11/24. 11 | // Copyright © 2017年 song. All rights reserved. 12 | // 13 | 14 | #import "UIViewController+Traking.h" 15 | #import 16 | @implementation UIViewController (Traking) 17 | 18 | + (void)load{ 19 | static dispatch_once_t onceToken; 20 | dispatch_once(&onceToken, ^{ 21 | Class class = [self class]; 22 | SEL orginSelector = @selector(viewWillAppear:); 23 | SEL swizzedSelector = @selector(TrackingViewWillAppear:); 24 | 25 | Method orginMethod = class_getInstanceMethod(class, orginSelector); 26 | Method swizzedMethod = class_getInstanceMethod(class, swizzedSelector); 27 | 28 | BOOL didAddMethod = class_addMethod(class, orginSelector, method_getImplementation(swizzedMethod), method_getTypeEncoding(swizzedMethod)); 29 | if (didAddMethod) { 30 | class_replaceMethod(class, swizzedSelector, method_getImplementation(orginMethod), method_getTypeEncoding(orginMethod)); 31 | }else{ 32 | method_exchangeImplementations(orginMethod, swizzedMethod); 33 | } 34 | }); 35 | } 36 | 37 | - (void)TrackingViewWillAppear:(BOOL)animated{ 38 | [self TrackingViewWillAppear:animated]; 39 | NSLog(@"TrackingViewWillAppear:%@",self); 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /runtime知识点/MyObject.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | // 6 | // MyObject.m 7 | // runtime知识点 8 | // 9 | // Created by songqingbo on 2017/11/22. 10 | // Copyright © 2017年 song. All rights reserved. 11 | // 12 | 13 | #import "MyObject.h" 14 | #import 15 | @interface MyObject (){ 16 | NSInteger _instance1; 17 | NSString *_instance2; 18 | } 19 | 20 | @property(assign,nonatomic) NSInteger intrger; 21 | 22 | - (void)method3WithArge1:(NSInteger)arge1 arge2:(NSString *)arge2; 23 | 24 | @end 25 | @implementation MyObject 26 | 27 | 28 | -(void)classMethod1{ 29 | 30 | 31 | } 32 | 33 | - (void)method1{ 34 | NSLog(@"cell Method1"); 35 | } 36 | 37 | - (void)method2{ 38 | 39 | } 40 | 41 | - (void)method3WithArge1:(NSInteger)arge1 arge2:(NSString *)arge2{ 42 | NSLog(@"arge1 %ld arge2 %@",arge1,arge2); 43 | } 44 | 45 | + (BOOL)resolveInstanceMethod:(SEL)sel{ 46 | NSString *selector = NSStringFromSelector(sel); 47 | if ([selector isEqualToString:@"method1"]) { 48 | class_addMethod(self.class, @selector(method1), (IMP)functionForMethod1, "@:"); 49 | } 50 | return [super resolveInstanceMethod:sel]; 51 | } 52 | 53 | void functionForMethod1(id self,SEL _cmd){ 54 | NSLog(@"%@ %p",self,_cmd); 55 | } 56 | 57 | - (instancetype)forwardingTargetForSelector:(SEL)aSelector{ 58 | NSString *selector = NSStringFromSelector(aSelector); 59 | if([selector isEqualToString:@"method2"]){ 60 | // return; 61 | } 62 | return [super forwardingTargetForSelector:aSelector]; 63 | } 64 | 65 | - (void)forwardInvocation:(NSInvocation *)anInvocation{ 66 | 67 | 68 | } 69 | 70 | 71 | @end 72 | -------------------------------------------------------------------------------- /runtime知识点/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /runtime知识点/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /runtime知识点/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /runtime知识点/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /runtime知识点/UIView+SQBView.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | // 8 | // UIView+SQBView.m 9 | // runtime知识点 10 | // 11 | // Created by songqingbo on 2017/11/23. 12 | // Copyright © 2017年 song. All rights reserved. 13 | // 14 | 15 | #import "UIView+SQBView.h" 16 | #import 17 | 18 | static char SQBTap;// 设置的key 19 | @implementation UIView (SQBView) 20 | 21 | - (void)setTapActionWithBlock:(void (^)(void))block{ 22 | /* 23 | 关联对象 关联对象可以是通过给定的key关联到类的实例上 24 | 同时也要指定一个内存管理策略 25 | */ 26 | // 获取关联对象
 27 | UITapGestureRecognizer *tap = objc_getAssociatedObject(self, &SQBTap); 28 | if(!tap){ 29 | tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForTap:)]; 30 | [self addGestureRecognizer:tap]; 31 | /* 32 | 设置关联对象
 33 | object 关联 34 | key 设置的key 注:key要用这种写法static char SQBTap 35 | value 关联的对象 36 | objc_AssociationPolicy 内存策略 37 | OBJC_ASSOCIATION_ASSIGN = 0, //弱引用,对象销毁不会造成关联对象的引用计数变化 38 | OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, //强引用,不支持多线程安全 39 | OBJC_ASSOCIATION_COPY_NONATOMIC = 3, //深拷贝,不支持多线程安全 40 | OBJC_ASSOCIATION_RETAIN = 01401, //强引用支持线程安全 41 | OBJC_ASSOCIATION_COPY = 01403 //深拷贝,支持线程安全 42 | 注: ,如果我们使用同一个key来关联另外一个对象时,也会自动释放之前关联的对象,这种情况下,先前的关联对象会被妥善地处理掉,并且新的对象会使用它的内存。 43 | 我们可以使用objc_removeAssociatedObjects函数来移除一个关联对象,或者使用objc_setAssociatedObject函数将key指定的关联对象设置为nil。 44 | // void objc_setAssociatedObject ( id object, const void *key, id value, objc_AssociationPolicy policy ); 45 | */ 46 | //设置关联对象 47 | objc_setAssociatedObject(self, &SQBTap, tap,OBJC_ASSOCIATION_RETAIN_NONATOMIC); 48 | } 49 | //设置的关联对象与block相关联 50 | objc_setAssociatedObject(self, &SQBTap, block, OBJC_ASSOCIATION_COPY); 51 | 52 | } 53 | 54 | - (void)actionForTap:(UITapGestureRecognizer *)tap{ 55 | if (tap.state == UIGestureRecognizerStateRecognized) { 56 | void(^action)(void) = objc_getAssociatedObject(self, &SQBTap); 57 | if (action) { 58 | action(); 59 | } 60 | } 61 | } 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /runtime知识点/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /runtime知识点/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // runtime知识点 4 | // 5 | // Created by songqingbo on 2017/11/22. 6 | // Copyright © 2017年 song. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "MyObject.h" 11 | #import "UIView+SQBView.h" 12 | #import 13 | @interface ViewController () 14 | 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // 1.类相关函数 22 | // 得到类名 ViewController 23 | NSLog(@"%s",class_getName([self class])); 24 | // 父类类名 25 | // This is superClass 0x10f426da0 isa指针地址 26 | NSLog(@"This is superClass %p",class_getSuperclass([self class])); 27 | 28 | // 是否是元类 29 | // Is MateClass 0 30 | NSLog(@"Is MateClass %d",class_isMetaClass([self class])); 31 | // 实例变量大小 32 | // 848 33 | size_t a = class_getInstanceSize([self class]); 34 | NSLog(@"%ld",a); 35 | unsigned int outCount = 0; 36 | MyObject *objtect = [[MyObject alloc] init]; 37 | Class cls = objtect.class; 38 | 39 | //类名 40 | NSLog(@"Class name %s",class_getName(cls)); 41 | //父类名字 42 | NSLog(@"SuperClass name %s",class_getName(class_getSuperclass(cls))); 43 | //是否元类 44 | NSLog(@"Is MetaClass %@",(class_isMetaClass(cls))?@"":@"NOT"); 45 | //变量大小 46 | NSLog(@"MyObject size %ld",class_getInstanceSize(cls)); 47 | //变量信息 48 | Ivar string = class_getInstanceVariable(cls, "_string"); 49 | 50 | if (string != NULL) { 51 | NSLog(@"MyObject instance messge %s",ivar_getName(string)); 52 | } 53 | // 属性操作 54 | objc_property_t *v = class_copyPropertyList(cls, &outCount); 55 | for (NSInteger i = 0; i< outCount; i++) { 56 | objc_property_t property = v[i]; 57 | NSLog(@"property is %s ",property_getName(property)); 58 | } 59 | free(v); 60 | 61 | //成员变量 62 | Ivar *vars = class_copyIvarList(cls, &outCount); 63 | for (NSInteger i =0; i< outCount; i++) { 64 | Ivar ivar = vars[i]; 65 | NSLog(@"copyIvarList is %s is %ld",ivar_getName(ivar),i); 66 | } 67 | free(vars); 68 | //方法操作 69 | Method *methods = class_copyMethodList(cls, &outCount); 70 | for (NSInteger i =0; i< outCount; i++) { 71 | Method met = methods[i]; 72 | #pragma clang diagnostic ignored"-Wformat" 73 | NSLog(@"Method is %s",method_getName(met)); 74 | } 75 | free(methods); 76 | 77 | // Format specifies type 'char *' but the argument has type 'SEL _Nonnull' 78 | // 获取方法名 79 | Method method = class_getInstanceMethod(cls,@selector(method1)); 80 | NSLog(@"Method name is %s",method_getName(method)); 81 | // 判断方法是否存在 82 | #pragma clang diagnostic ignored"-Wundeclared-selector" 83 | NSLog(@"Have you method %d",class_respondsToSelector(cls, @selector(method3WithArge1:arge2:))); 84 | // 指向函数实现的指针,相当于方法的实现 85 | IMP imp = class_getMethodImplementation(cls, @selector(method1)); 86 | imp(); 87 | //动态创建类 88 | #pragma clang diagnostic ignored" -Wunused-variable" 89 | // 注:运行时规定,只能在objc_allocateClassPair与objc_registerClassPair两个函数之间为类添加变量 90 | // 1.添加一个自定义的类 类名是MySubClass 91 | // 父类class,类名,额外空间 92 | Class myClass = objc_allocateClassPair(objtect.class, "MySubClass", 0); 93 | // 2.增加方法,交换方法 94 | //注: v@: 意思是 v是void @:没有返回参数 95 | if( class_addMethod(myClass, @selector(mysubMethod1),(IMP)mysubMethod1, "v@:")){ 96 | class_replaceMethod(myClass, @selector(method1), (IMP)mysubMethod1,"v@:"); 97 | } 98 | 99 | /* 100 | 3.增加一个NSSsting类型属性 属性名myString 101 | 变量size sizeof(NSString) 102 | 对齐 指针类型的为log2(sizeof(NSString*)) 103 | 类型 @encode(NSString*) 104 | class_addIvar(class,变量名,变量size,对齐,类型) 105 | */ 106 | //添加同名属性会失败 107 | BOOL isd = class_addIvar(myClass, "_myString", sizeof(NSString *), log(sizeof(NSString *)), @encode(NSString *)); 108 | NSLog(@"属性是否添加成功 %d",isd); 109 | /* 110 | 特性相关编码 111 | 属性的特性字符串 以 T@encode(type) 开头, 以 V实例变量名称 结尾,中间以特性编码填充,通过property_getAttributes即可查看 112 | 特性编码 具体含义 113 | R readonly 114 | C copy 115 | & retain ARC strong 116 | N nonatomic 117 | G(name) getter=(name) 118 | S(name) setter=(name) 119 | D @dynamic 120 | W weak 121 | P 用于垃圾回收机制 122 | */ 123 | //@T 124 | objc_property_attribute_t type; 125 | type.name = "T"; 126 | type.value = @encode(NSString *); 127 | //copy 128 | objc_property_attribute_t owership = {"C",""}; 129 | //nonatomic 130 | objc_property_attribute_t oeership2 = {"N",""}; 131 | //V_属性名 132 | objc_property_attribute_t var = {"V","_myString"}; 133 | //特性数组 134 | objc_property_attribute_t attributes[] = {type,owership,oeership2,var}; 135 | //向类中添加名为myString的属性,属性的特性包含在attributes中 136 | class_addProperty(myClass, "myString", attributes, 4); 137 | unsigned int propertyCount; 138 | objc_property_t * properties = class_copyPropertyList(myClass, &propertyCount); 139 | for (int i = 0; i