From d5547e5413437514c0de636f14fc7b432fc527d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E6=B5=B7=E5=86=9B?= Date: Thu, 17 Oct 2019 14:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20nn=5Fassociated=5Fsetter?= =?UTF-8?q?=20=E4=B8=AD=E5=AD=98=E5=82=A8=20key=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NNMacros/NNMacros/NNMacrosAssociatedSetter.h | 31 ++++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/NNMacros/NNMacros/NNMacrosAssociatedSetter.h b/NNMacros/NNMacros/NNMacrosAssociatedSetter.h index 75fec64..a229d1a 100644 --- a/NNMacros/NNMacros/NNMacrosAssociatedSetter.h +++ b/NNMacros/NNMacros/NNMacrosAssociatedSetter.h @@ -70,14 +70,14 @@ static inline objc_AssociationPolicy nn_associated_setter_store_policy(NSString /** - 获取 setter 方法中 objc_setAssociatedObject 参数 key + 获取 getter 方法的 sel - @discussion 动态添加获取属性的 key 值为 getter 方法的 sel ,该函数通过截取 setter 方法的 sel ,获取 key 值 + @discussion 函数通过截取 setter 方法的 sel ,获取 getter 方法的 sel @param cls 类名 @param sel setter 方法的 sel - @return setter 方法中 objc_setAssociatedObject 参数 key + @return getter 方法的 sel */ -static inline SEL nn_associated_setter_store_key(Class cls, SEL sel) { +static inline SEL nn_associated_getter_sel(Class cls, SEL sel) { SEL ret = nil; const char *selName = sel_getName(sel); const char *prefix = "set"; @@ -86,25 +86,24 @@ static inline SEL nn_associated_setter_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; } #define nn_associated_setter_key(clazz, setter) \ - nn_associated_setter_store_key(clazz, setter)\ + nn_associated_getter_sel(clazz, setter)\ #define nn_associated_setter_policy(arc_type, atomic_type) \ nn_associated_setter_store_policy(NN_arg2String(arc_type), NN_arg2String(atomic_type)) \