Skip to content

Commit

Permalink
修改 nn_categorysetter_store_key 中存储 key 获取失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
顾海军 committed Oct 17, 2019
1 parent c6d0dce commit 505d48e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions NNMacros/NNMacros/NNMacrosCategorySetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,19 @@ static inline SEL nn_categorysetter_store_key(Class cls, SEL sel) {
// 2. 判断 sel 是否是以 ":" 结尾
if (':' != selName[strlen(selName) - 1]) { return ret; }
// 3. 拷贝 "set" 之后的字符串,包括 "\0"。
size_t keyNameSize = (strlen(selName) + 1) - strlen(prefix);
unsigned char *keyName = (unsigned char *)calloc(keyNameSize, sizeof(unsigned char));
memcpy(keyName, (selName + strlen(prefix)), keyNameSize);
size_t keySize = (strlen(selName) + 1) - strlen(prefix);
unsigned char *key = (unsigned char *)calloc(keySize, sizeof(unsigned char));
memcpy(key, (selName + strlen(prefix)), keySize);
// 4. 如果 "set" 是字母,那么转换为小写。如:"setUserName:",将 "UserName:" 转为 "userName:" 。
if (keyName[0] > 'A' && keyName[0] < 'Z') { keyName[0] += ('a' - 'A'); }
if (key[0] > 'A' && key[0] < 'Z') { key[0] += ('a' - 'A'); }
// 5. 去掉 sel 结尾的 ":" ,使用 "\0" 替换。
keyName[strlen((const char *)keyName) - 1] = '\0';
// 6. 避免仅有 setter 方法的情况,通过 method 获取 setter 方法 sel 。(实际应用中通常不会出现)
SEL keySel = NSSelectorFromString([NSString stringWithUTF8String:(const char *)keyName]);
Method method = class_getInstanceMethod(cls, keySel);
if (method == nil) { goto end; }
ret = method_getName(method) ;
key[strlen((const char *)key) - 1] = '\0';
// 6. 获取 getter 方法的 sel
if (strlen((const char *)key)) {
ret = sel_getUid(key);
}
end:
free(keyName);
free(key);
return ret;
}

Expand Down

0 comments on commit 505d48e

Please sign in to comment.