123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- //
- // RQReflection.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/16.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "RQReflection.h"
- #import <objc/runtime.h>
- SEL RQSelectorWithKeyPattern(NSString *key, const char *suffix) {
- NSUInteger keyLength = [key maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- NSUInteger suffixLength = strlen(suffix);
-
- char selector[keyLength + suffixLength + 1];
-
- BOOL success = [key getBytes:selector maxLength:keyLength usedLength:&keyLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, key.length) remainingRange:NULL];
- if (!success) return NULL;
-
- memcpy(selector + keyLength, suffix, suffixLength);
- selector[keyLength + suffixLength] = '\0';
-
- return sel_registerName(selector);
- }
- SEL RQSelectorWithCapitalizedKeyPattern(const char *prefix, NSString *key, const char *suffix) {
- NSUInteger prefixLength = strlen(prefix);
- NSUInteger suffixLength = strlen(suffix);
-
- NSString *initial = [key substringToIndex:1].uppercaseString;
- NSUInteger initialLength = [initial maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
-
- NSString *rest = [key substringFromIndex:1];
- NSUInteger restLength = [rest maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
-
- char selector[prefixLength + initialLength + restLength + suffixLength + 1];
- memcpy(selector, prefix, prefixLength);
-
- BOOL success = [initial getBytes:selector + prefixLength maxLength:initialLength usedLength:&initialLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, initial.length) remainingRange:NULL];
- if (!success) return NULL;
-
- success = [rest getBytes:selector + prefixLength + initialLength maxLength:restLength usedLength:&restLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, rest.length) remainingRange:NULL];
- if (!success) return NULL;
-
- memcpy(selector + prefixLength + initialLength + restLength, suffix, suffixLength);
- selector[prefixLength + initialLength + restLength + suffixLength] = '\0';
-
- return sel_registerName(selector);
- }
|