Arrangement of runtime usage method in IOS

  • 2021-12-12 06:02:35
  • OfStack

Arrangement of runtime usage method in IOS

Friends who do iOS know or hear about runtime, which is very similar to the reflection mechanism of java, but its function is far better than that of java. Through runtime, we can dynamically add properties, member variables, methods to a class, and access them by reading and writing.

Create two new classes ClassOne and ClassTwo


#import <Foundation/Foundation.h>

@interface ClassOne : NSObject{
  NSString *_publicVar1;
  NSString *_publicVar2;
}

@property(nonatomic,copy) NSString *publicProperty1;
@property(nonatomic,copy) NSString *publicProperty2;

- (void) testClassOneWithArg1:(NSString *)arg1;
@end


#import "ClassOne.h"

@interface ClassOne()
@property(nonatomic,copy) NSString *privateProperty1;
@property(nonatomic,copy) NSString *privateProperty2;

@end

@implementation ClassOne{
    NSString *_privateVar1;
    NSString *_privateVar2;
}

- (void)testClassOneWithArg1:(NSString *)arg1{
  NSLog(@"this is CalssOne, arg1:%@",arg1);
}

- (void)testClassOneWithArg1:(NSString *)arg1 arg2:arg2{
  NSLog(@"this is CalssOne, arg1:%@ arg2:%@",arg1,arg2);
}
@end


#import <Foundation/Foundation.h>

@interface ClassTwo : NSObject
- (void) testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2;
@end


#import "ClassTwo.h"

@implementation ClassTwo
- (void)testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2{
  NSLog(@"this is ClassTwo arg1:%@,arg2:%@",arg1,arg2);
}
@end

1. Copy the object


ClassOne *one = [ClassOne new];
id onec1 = object_copy(one,sizeof(one));

2. Add methods to classes


ClassOne *one = [ClassOne new];
class_addMethod([ClassOne class], @selector(testClassOneWithArg1:arg2:arg3:), (IMP)testClassOne , "i@:@@@");
[one testClassOneWithArg1:@"arg1" arg2:@"arg2" arg3:@"arg3"];

// Method corresponding to the C Function 
int testClassOne(id self,SEL _cmd, NSString *arg1,NSString *arg2,NSString *arg3){
NSLog(@"this is a test function add to ClassOne as a methad with arg1:%@ arg2:%@ and arg3:%@",arg1,arg2,arg3);
  return 10;
}

3. Add Attributes (Mode 1)


// Attribute type 
objc_property_attribute_t type = { "T", "@\"NSString\"" };
// Access type 
objc_property_attribute_t ownership = { "C", "" };
// Corresponding member variable name 
objc_property_attribute_t backingivar = { "V", "_testPropertyName" };
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
class_addProperty([ClassOne class], "testPropertyName", attrs, 3);
class_addMethod([ClassOne class], @selector(testPropertyName), (IMP)testPropertyNameGetter , "@:@@");
class_addMethod([ClassOne class], @selector(setTestPropertyName:), (IMP)testPropertyNameSetter, "v:@@@");


// Property corresponding to the Getter Method 
NSString* testPropertyNameGetter(id self,SEL _cmd){
  Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");
  return object_getIvar(self, ivar);
}

// Property corresponding to the Setter Method 
void testPropertyNameSetter(id self,SEL _cmd,NSString *testPropertyNameValue){
  Ivar ivar = class_getInstanceVariable([ClassOne class], "_testPropertyName");
  object_setIvar(self, ivar, testPropertyNameValue);
}

4. Add Attributes (Mode 2)


ClassOne *one = [ClassOne new];
objc_setAssociatedObject(one, "objTag", @"value", OBJC_ASSOCIATION_COPY);
NSString *value = objc_getAssociatedObject(one, "objTag");
NSLog(@" Pass Associate Settings :%@",value);

5. Get the name of the class


ClassOne *one = [ClassOne new];
const char *className = object_getClassName(one);
NSLog(@"className:%@",[NSString stringWithUTF8String:className]);

6. Get all the methods of a class


UInt count;
Method *methods = class_copyMethodList([ClassOne class], &count);
for (int i = 0; i < count; i++) {
  Method method = methods[i];
  SEL sel = method_getName(method);
  NSLog(@" Method name :%@",NSStringFromSelector(sel));
}

7. Get all the properties of a class


uint propertyCount;
objc_property_t *ps = class_copyPropertyList([ClassOne class], &propertyCount);
for (uint i = 0; i < propertyCount; i++) {
  objc_property_t property = ps[i];
  const char *propertyName = property_getName(property);
  const char *propertyAttributes = property_getAttributes(property);
  NSLog(@"propertyName:%@",[NSString stringWithUTF8String:propertyName]);
  NSLog(@"propertyAttributes:%@",[NSString stringWithUTF8String:propertyAttributes]);
}

8. Get all the member variables of the class


uint ivarCount;
Ivar *ivars = class_copyIvarList([ClassOne class], &ivarCount);
for (uint i = 0; i < ivarCount; i++) {
  Ivar ivar = ivars[i];
  const char *ivarName = ivar_getName(ivar);
  NSLog(@"ivarName:%@",[NSString stringWithUTF8String:ivarName]);
}

9. Get member variable types


#import <Foundation/Foundation.h>

@interface ClassTwo : NSObject
- (void) testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2;
@end


#import "ClassTwo.h"

@implementation ClassTwo
- (void)testClassTwoWithArg1:(NSString *)arg1 arg2:(NSString *)arg2{
  NSLog(@"this is ClassTwo arg1:%@,arg2:%@",arg1,arg2);
}
@end

0

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: