美文网首页
OC&Swift runtime 一 Protocol & pr

OC&Swift runtime 一 Protocol & pr

作者: 527267线的iOS工程师 | 来源:发表于2018-07-07 10:26 被阅读0次

方法一:objc_copyImageNames
OC

const char * _Nonnull * objc_copyImageNames(unsigned int *outCount);
const char * imageNames = *objc_copyImageNames(&i);
NSString *imageNamesString = [NSString stringWithCString:imageNames encoding:kCFStringEncodingUTF8];
NSArray *imageNamesStringArray = [[imageNamesString stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""] componentsSeparatedByString:@"/"];
int imageNameTmp = 0;
while (imageNameTmp < imageNamesStringArray.count) {
    NSLog(@"%@", imageNamesStringArray[imageNameTmp]);
    imageNameTmp ++;
}

方法二:class_getImageName
OC

const char * class_getImageName(Class cls);
NSArray *imageNameArray = [[[NSString stringWithCString:class_getImageName([self class]) encoding:kCFStringEncodingUTF8] stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""] componentsSeparatedByString:@"/"];

方法三:objc_copyClassNamesForImage
OC

const char * _Nonnull * objc_copyClassNamesForImage(const char *image, unsigned int *outCount);
NSLog(@"%s", *objc_copyClassNamesForImage(class_getImageName(NSClassFromString(@"UIView")), &i));

方法四:sel_getName
OC

const char * sel_getName(SEL sel);
NSLog(@"%s", sel_getName(@selector(load)));

方法五:sel_registerName
OC
Discussion:sel——registerName就相当于@selector()

SEL sel_registerName(const char *str);
SEL loadSel = sel_registerName("load");

方法六:sel_getUid
OC

SEL sel_getUid(const char *str);
SEL loadSel = sel_getUid("load");

方法七:sel_isEqual
OC

BOOL sel_isEqual(SEL lhs, SEL rhs);
NSLog(@"%d", sel_isEqual(@selector(nsLogMySon), @selector(nslogCopy)));

方法八:objc_getProtocol
OC

Protocol * objc_getProtocol(const char *name);
NSLog(@"%s", protocol_getName(objc_getProtocol("UITableViewDelegate")));

方法九:objc_copyProtocolList
OC

Protocol * _Nonnull * objc_copyProtocolList(unsigned int *outCount);
int protocolCount = 0;
Protocol * _Nonnull __unsafe_unretained *protocolList = class_copyProtocolList([self class], &protocolCount);
j = 0;
while (j < protocolCount) {
    j ++;
    NSLog(@"%s", protocol_getName(*protocolList));
    protocolList ++;
}
free((__bridge void *)(*protocolList));

方法十:objc_allocateProtocol
OC

Protocol * objc_allocateProtocol(const char *name);
Protocol *NewProtocol = objc_allocateProtocol("DaoXiangProtocol");

方法十一:objc_registerProtocol
OC

void objc_registerProtocol(Protocol *proto);

方法十二:protocol_addMethodDescription
Discussion: 要写在objc_registerProtocol之前
OC

void protocol_addMethodDescription(Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod);
protocol_addMethodDescription(NewProtocol, @selector(nslogCopy), "v@:", YES, YES);

方法十三:protocol_addProtocol
Discussion: 要写在objc_registerProtocol之前
OC

void protocol_addProtocol(Protocol *proto, Protocol *addition);
protocol_addProtocol(NewProtocol, objc_getProtocol("UITableViewDelegate"));

方法十四:protocol_addProperty
Discussion: 要写在objc_registerProtocol之前
OC

void protocol_addProperty(Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);
objc_property_attribute_t nonatomic = {"N", ""};
objc_property_attribute_t strong = {"&", ""};
objc_property_attribute_t type = {"T", "@\"NSString\""};
objc_property_attribute_t ivar = {"V", "_qqqqq"};
objc_property_attribute_t attributes[] = {nonatomic, strong, type, ivar};
protocol_addProperty(NewProtocol, "property", attributes, 4, YES, YES);

方法十五:protocol_getName
OC

const char * protocol_getName(Protocol *proto);
NSLog(@"%s", protocol_getName(objc_getProtocol("UITableViewDelegate")));

方法十六:protocol_isEqual
OC

BOOL protocol_isEqual(Protocol *proto, Protocol *other);
NSLog(@"%d", protocol_isEqual(NewProtocol, objc_getProtocol("UITableViewDelegate")));

方法十七:protocol_copyMethodDescriptionList
OC

struct objc_method_description * protocol_copyMethodDescriptionList(Protocol *proto, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount);
struct objc_method_description *protocolMethodList = protocol_copyMethodDescriptionList(objc_getProtocol("UITableViewDelegate"), NO, YES, &i);
for (int temp = 0; temp < i; protocolMethodList++, temp++) {
    struct objc_method_description method = *protocolMethodList;
    NSLog(@"%@", NSStringFromSelector(method.name));
}

方法十八:protocol_getMethodDescription
OC

struct objc_method_description protocol_getMethodDescription(Protocol *proto, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod);
struct objc_method_description protocolMethod = protocol_getMethodDescription(objc_getProtocol("UITableViewDelegate"), NSSelectorFromString(@"tableView:canFocusRowAtIndexPath:"), NO, YES);
NSLog(@"%@", NSStringFromSelector(protocolMethod.name));

方法十九:protocol_copyPropertyList
OC

objc_property_t  _Nonnull * protocol_copyPropertyList(Protocol *proto, unsigned int *outCount);
objc_property_t *propertyList = protocol_copyPropertyList(NewProtocol, &i);
for (int j = 0; j < i; j++, propertyList++) {
    NSLog(@"%s",property_getName(*propertyList));
}
free(*propertyList);

方法二十:protocol_copyPropertyList2
OC

propertyList = protocol_copyPropertyList2(NewProtocol, &i, YES, YES);
for (int j = 0; j < i; j++, propertyList++) {
    NSLog(@"%s",property_getName(*propertyList));
}
free(*propertyList);

方法二十一:protocol_getProperty
OC

objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty);
NSLog(@"%s", property_getName(protocol_getProperty(NewProtocol, "property", YES, YES)));

方法二十二:protocol_copyProtocolList
OC

Protocol * _Nonnull __unsafe_unretained *protocolProtocolList = protocol_copyProtocolList(NewProtocol, &i);
for (int j = 0; j < i; j++, protocolProtocolList++) {
    NSLog(@"%s", protocol_getName((*protocolProtocolList)));
}

方法二十三:protocol_conformsToProtocol
OC

BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other);
bool iqwe = protocol_conformsToProtocol(NewProtocol, objc_getProtocol("UITableViewDelegate"));

方法二十四:property_getName
OC

const char * property_getName(objc_property_t property);
NSLog(@"%s", property_getName(protocol_getProperty(NewProtocol, "property", YES, YES)));

方法二十五:property_getAttributes
OC

const char * property_getAttributes(objc_property_t property);
NSLog(@"%s", property_getAttributes(*propertyList));

方法二十六:property_copyAttributeValue
OC

char * property_copyAttributeValue(objc_property_t property, const char *attributeName);
NSLog(@"%s", property_copyAttributeValue(*propertyList, "V"));

方法二十七:property_copyAttributeList
OC

objc_property_attribute_t * property_copyAttributeList(objc_property_t property, unsigned int *outCount);
unsigned int count;
objc_property_attribute_t *attributeList = property_copyAttributeList(*propertyList, &count);
NSLog(@"%s", (*attributeList).name);

方法二十八:objc_enumerationMutation
Discussion: 在获取enumState.itemsPtr中每个元素前,都检查一遍enumState.mutationsPtr所指标志是否改变,改变则抛出异常
对__NSArrayI,enumState.mutationsPtr指向一个静态局部变量,永远也不会抛异常
对__NSArrayM,enumState.mutationsPtr指向_mutations变量, 每次增删操作后,_mutations会+1
OC

void objc_enumerationMutation(id obj);
NSArray *arr;
id l_collection = (id) arr;
objc_enumerationMutation(l_collection);

方法二十九:imp_implementationWithBlock
OC

IMP imp_implementationWithBlock(id block);
imp_implementationWithBlock(block)

方法三十:imp_getBlock

id imp_getBlock(IMP anImp);
newBlock = imp_getBlock(imp_implementationWithBlock(block));
newBlock();

方法三十一:imp_removeBlock

BOOL imp_removeBlock(IMP anImp);
imp_removeBlock(imp_implementationWithBlock(block));

方法三十二:objc_loadWeak
OC

id objc_loadWeak(id  _Nullable *location);
__weak NSNumber *myWeak = @0;
NSNumber *newWeak = objc_loadWeak(&myWeak);

方法三十三:objc_storeWeak
OC

id objc_storeWeak(id  _Nullable *location, id obj);
NSNumber *aaa = objc_storeWeak(&myWeak, @1);

相关文章

网友评论

      本文标题:OC&Swift runtime 一 Protocol & pr

      本文链接:https://www.haomeiwen.com/subject/rcxgyftx.html