Skip to content

Commit

Permalink
修改 synthesize getter setter 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
顾海军 committed Oct 16, 2019
1 parent 1bfd5d1 commit 411d0da
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 368 deletions.
12 changes: 0 additions & 12 deletions NNMacros/NNMacros.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
670F8DBB20C121B4004CB3E9 /* metamacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 670F8DAF20C121B4004CB3E9 /* metamacros.h */; settings = {ATTRIBUTES = (Public, ); }; };
67AA435A2277F9B000FEE272 /* NNMacrosCocoapodsFix.h in Headers */ = {isa = PBXBuildFile; fileRef = 67AA43582277F9B000FEE272 /* NNMacrosCocoapodsFix.h */; };
67AA435B2277F9B000FEE272 /* NNMacrosCocoapodsFix.m in Sources */ = {isa = PBXBuildFile; fileRef = 67AA43592277F9B000FEE272 /* NNMacrosCocoapodsFix.m */; };
7A4833302356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A48332E2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.h */; };
7A4833312356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A48332F2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.m */; };
7A48335E2356DCE500ACE8E1 /* NNMacrosAssociatedKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A4833322356BB6400ACE8E1 /* NNMacrosAssociatedKey.h */; settings = {ATTRIBUTES = (Public, ); }; };
7A48335F2356DCE500ACE8E1 /* NNMacrosAssociatedGetter.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A48332C2356B06D00ACE8E1 /* NNMacrosAssociatedGetter.h */; settings = {ATTRIBUTES = (Public, ); }; };
7A4833602356DCE500ACE8E1 /* NNMacrosAssociatedSetter.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A48332D2356B07900ACE8E1 /* NNMacrosAssociatedSetter.h */; settings = {ATTRIBUTES = (Public, ); }; };
7A4833612356DCE500ACE8E1 /* NNMacrosAssociatedSynthesize.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A48332B2356B04500ACE8E1 /* NNMacrosAssociatedSynthesize.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -59,9 +56,6 @@
7A48332B2356B04500ACE8E1 /* NNMacrosAssociatedSynthesize.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NNMacrosAssociatedSynthesize.h; sourceTree = "<group>"; };
7A48332C2356B06D00ACE8E1 /* NNMacrosAssociatedGetter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NNMacrosAssociatedGetter.h; sourceTree = "<group>"; };
7A48332D2356B07900ACE8E1 /* NNMacrosAssociatedSetter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NNMacrosAssociatedSetter.h; sourceTree = "<group>"; };
7A48332E2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSObject+NNMacrosAssociatedSynthesize.h"; sourceTree = "<group>"; };
7A48332F2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSObject+NNMacrosAssociatedSynthesize.m"; sourceTree = "<group>"; };
7A4833322356BB6400ACE8E1 /* NNMacrosAssociatedKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NNMacrosAssociatedKey.h; sourceTree = "<group>"; };
7AD9042B233DCCF100A4ABF8 /* NNMacrosSandbox.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NNMacrosSandbox.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -107,12 +101,9 @@
children = (
670F8D8C20C120FA004CB3E9 /* NNMacros.h */,
670F8DA520C121B3004CB3E9 /* NNMacrosApp.h */,
7A4833322356BB6400ACE8E1 /* NNMacrosAssociatedKey.h */,
7A48332C2356B06D00ACE8E1 /* NNMacrosAssociatedGetter.h */,
7A48332D2356B07900ACE8E1 /* NNMacrosAssociatedSetter.h */,
7A48332B2356B04500ACE8E1 /* NNMacrosAssociatedSynthesize.h */,
7A48332E2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.h */,
7A48332F2356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.m */,
670F8DA920C121B4004CB3E9 /* NNMacrosLazyGetter.h */,
670F8DA420C121B3004CB3E9 /* NNMacrosDevice.h */,
670F8DAA20C121B4004CB3E9 /* NNMacrosColor.h */,
Expand Down Expand Up @@ -167,15 +158,13 @@
670F8DB720C121B4004CB3E9 /* NNMacrosColor.h in Headers */,
670F8DB620C121B4004CB3E9 /* NNMacrosLazyGetter.h in Headers */,
670F8D9A20C120FA004CB3E9 /* NNMacros.h in Headers */,
7A48335E2356DCE500ACE8E1 /* NNMacrosAssociatedKey.h in Headers */,
7A48335F2356DCE500ACE8E1 /* NNMacrosAssociatedGetter.h in Headers */,
7A4833602356DCE500ACE8E1 /* NNMacrosAssociatedSetter.h in Headers */,
7A4833612356DCE500ACE8E1 /* NNMacrosAssociatedSynthesize.h in Headers */,
670F8DB220C121B4004CB3E9 /* NNMacrosApp.h in Headers */,
7A4833622356DD0E00ACE8E1 /* NNMacrosSandbox.h in Headers */,
670F8DB820C121B4004CB3E9 /* NNMacrosScreen.h in Headers */,
67AA435A2277F9B000FEE272 /* NNMacrosCocoapodsFix.h in Headers */,
7A4833302356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -276,7 +265,6 @@
buildActionMask = 2147483647;
files = (
67AA435B2277F9B000FEE272 /* NNMacrosCocoapodsFix.m in Sources */,
7A4833312356B0B000ACE8E1 /* NSObject+NNMacrosAssociatedSynthesize.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 0 additions & 2 deletions NNMacros/NNMacros/NNMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ FOUNDATION_EXPORT const unsigned char NNMacrosVersionString[];

#if __has_include(<NNMacros/NNMacros.h>)
#import <NNMacros/NNMacrosLazyGetter.h>
#import <NNMacros/NNMacrosAssociatedKey.h>
#import <NNMacros/NNMacrosAssociatedGetter.h>
#import <NNMacros/NNMacrosAssociatedSetter.h>
#import <NNMacros/NNMacrosAssociatedSynthesize.h>
Expand All @@ -31,7 +30,6 @@ FOUNDATION_EXPORT const unsigned char NNMacrosVersionString[];
#import <NNMacros/NNMacrosVersion.h>
#else
#import "NNMacrosLazyGetter.h"
#import "NNMacrosAssociatedKey.h"
#import "NNMacrosAssociatedGetter.h"
#import "NNMacrosAssociatedSetter.h"
#import "NNMacrosAssociatedSynthesize.h"
Expand Down
11 changes: 5 additions & 6 deletions NNMacros/NNMacros/NNMacrosAssociatedGetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#include "metamacros.h"
#include "NNMacrosAssociatedKey.h"

/***************************************************
associated getter
Expand All @@ -21,7 +20,7 @@
atomic_type: atomic, nonatomic
arc_type: assign, strong, copy
data_type: 属性类型
param0: 属性名称
param0: getter方法名
param1: hook_after_load
param2: hook_befor_ret
*/
Expand Down Expand Up @@ -87,7 +86,7 @@ static inline char nn_associated_getter_ret_encoding(Class cls, SEL sel) {
\
- (data_type)metamacro_at(0, __VA_ARGS__) \
{\
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = _cmd; \
id __obj = objc_getAssociatedObject(self, __key); \
metamacro_at(1, __VA_ARGS__) \
data_type __ivar = __obj; \
Expand All @@ -101,10 +100,10 @@ static inline char nn_associated_getter_ret_encoding(Class cls, SEL sel) {
\
- (data_type)metamacro_at(0, __VA_ARGS__) \
{\
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = _cmd; \
NSMapTable *__table = objc_getAssociatedObject(self, __key); \
metamacro_at(1, __VA_ARGS__) \
id __obj = [__table objectForKey:@(__key)]; \
id __obj = [__table objectForKey:NSStringFromSelector(__key)]; \
data_type __ivar = __obj; \
metamacro_at(2, __VA_ARGS__) \
return __ivar; \
Expand All @@ -116,7 +115,7 @@ static inline char nn_associated_getter_ret_encoding(Class cls, SEL sel) {
\
- (data_type)metamacro_at(0, __VA_ARGS__) \
{\
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = _cmd; \
id __obj = objc_getAssociatedObject(self, __key); \
metamacro_at(1, __VA_ARGS__) \
char __encoding = nn_associated_getter_encoding([self class], _cmd); \
Expand Down
19 changes: 0 additions & 19 deletions NNMacros/NNMacros/NNMacrosAssociatedKey.h

This file was deleted.

63 changes: 50 additions & 13 deletions NNMacros/NNMacros/NNMacrosAssociatedSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#include "metamacros.h"
#include "NNMacrosAssociatedKey.h"

/***************************************************
associated setter
Expand All @@ -21,14 +20,13 @@
atomic_type: atomic, nonatomic
arc_type: assign, strong, copy
data_type: 属性类型
param0: 属性名称
param0: setter方法名
param1: hook_begin_set
param2: hook_befor_store
*/

#ifndef nn_associated_setter


#pragma mark - setter

#define nn_associated_setter(atomic_type, arc_type, data_type, ...) \
Expand All @@ -38,6 +36,7 @@

#pragma mark - setter tools


/**
获取 setter 方法中 objc_setAssociatedObject 参数 policy
Expand Down Expand Up @@ -69,6 +68,44 @@ static inline objc_AssociationPolicy nn_associated_setter_store_policy(NSString
}
}


/**
获取 setter 方法中 objc_setAssociatedObject 参数 key
@discussion 动态添加获取属性的 key 值为 getter 方法的 sel ,该函数通过截取 setter 方法的 sel ,获取 key 值
@param cls 类名
@param sel setter 方法的 sel
@return setter 方法中 objc_setAssociatedObject 参数 key
*/
static inline SEL nn_associated_setter_store_key(Class cls, SEL sel) {
SEL ret = nil;
const char *selName = sel_getName(sel);
const char *prefix = "set";
// 1. 判断 sel 是否是以 "set" 开头
if (0 != memcmp(selName, prefix, strlen(prefix))) { return ret; }
// 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);
// 4. 如果 "set" 是字母,那么转换为小写。如:"setUserName:",将 "UserName:" 转为 "userName:" 。
if (keyName[0] > 'A' && keyName[0] < 'Z') { keyName[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) ;
end:
free(keyName);
return ret;
}

#define nn_associated_setter_key(clazz, setter) \
nn_associated_setter_store_key(clazz, setter)\

#define nn_associated_setter_policy(arc_type, atomic_type) \
nn_associated_setter_store_policy(NN_arg2String(arc_type), NN_arg2String(atomic_type)) \

Expand All @@ -80,10 +117,10 @@ static inline objc_AssociationPolicy nn_associated_setter_store_policy(NSString
#pragma mark - setter strong

#define nn_associated_setter_strong(atomic_type, arc_type, data_type, ...) \
@dynamic metamacro_at(0, __VA_ARGS__);\
- (void)metamacro_concat(nn_associated_setter_prefix, metamacro_at(0, __VA_ARGS__)):(data_type)newValue\
\
- (void)metamacro_at(0, __VA_ARGS__):(data_type)newValue\
{ \
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = nn_associated_setter_key([self class], _cmd); \
data_type __ivar = newValue; \
metamacro_at(1, __VA_ARGS__) \
id __obj = __ivar; \
Expand All @@ -94,25 +131,25 @@ static inline objc_AssociationPolicy nn_associated_setter_store_policy(NSString
#pragma mark - setter weak

#define nn_associated_setter_weak(atomic_type, arc_type, data_type, ...) \
@dynamic metamacro_at(0, __VA_ARGS__);\
- (void)metamacro_concat(nn_associated_setter_prefix, metamacro_at(0, __VA_ARGS__)):(data_type)newValue\
\
- (void)metamacro_at(0, __VA_ARGS__):(data_type)newValue\
{ \
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = nn_associated_setter_key([self class], _cmd); \
data_type __ivar = newValue; \
metamacro_at(1, __VA_ARGS__) \
NSMapTable *__obj = [NSMapTable strongToWeakObjectsMapTable]; \
[__obj setObject:__ivar forKey:@(__key)]; \
[__obj setObject:__ivar forKey:NSStringFromSelector(__key)]; \
metamacro_at(2, __VA_ARGS__) \
objc_setAssociatedObject(self, __key, __obj, nn_associated_setter_policy(arc_type, atomic_type)); \
} \

#pragma mark - setter assign

#define nn_associated_setter_assign(atomic_type, arc_type, data_type, ...) \
@dynamic metamacro_at(0, __VA_ARGS__);\
- (void)metamacro_concat(nn_associated_setter_prefix, metamacro_at(0, __VA_ARGS__)):(data_type)newValue\
\
- (void)metamacro_at(0, __VA_ARGS__):(data_type)newValue\
{ \
const char *__key = metamacro_stringify(metamacro_concat(nn_associated_store_key, metamacro_at(0, __VA_ARGS__))); \
SEL __key = nn_associated_setter_key([self class], _cmd); \
data_type __ivar = newValue; \
metamacro_at(1, __VA_ARGS__) \
id __obj = @(__ivar); \
Expand Down
9 changes: 5 additions & 4 deletions NNMacros/NNMacros/NNMacrosAssociatedSynthesize.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
atomic_type: atomic, nonatomic
arc_type: assign, strong, copy
data_type: 属性类型
property_name: 属性名称
getter: getter方法名
setter: setter方法名
*/

#ifndef nn_associated_synthesize

#define nn_associated_synthesize(atomic_type, arc_type, data_type, property_name) \
nn_associated_getter(atomic_type, arc_type, data_type, property_name); \
nn_associated_setter(atomic_type, arc_type, data_type, property_name); \
#define nn_associated_synthesize(atomic_type, arc_type, data_type, getter, setter) \
nn_associated_getter(atomic_type, arc_type, data_type, getter); \
nn_associated_setter(atomic_type, arc_type, data_type, setter); \

#endif /* nn_associated_synthesize */

Expand Down
13 changes: 0 additions & 13 deletions NNMacros/NNMacros/NSObject+NNMacrosAssociatedSynthesize.h

This file was deleted.

Loading

0 comments on commit 411d0da

Please sign in to comment.