Skip to content

Commit

Permalink
关联对象 Associated Object 的本质
Browse files Browse the repository at this point in the history
  • Loading branch information
pro648 committed Feb 5, 2021
1 parent 9f67316 commit 4540370
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 0 deletions.
293 changes: 293 additions & 0 deletions AssociatedObject/AssociatedObject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
0D2F0BF725C9068100ABC584 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D2F0BF625C9068100ABC584 /* main.m */; };
0D2F0C0025C906B200ABC584 /* Child.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D2F0BFF25C906B200ABC584 /* Child.m */; };
0D2F0C0425C906E500ABC584 /* Child+Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D2F0C0325C906E500ABC584 /* Child+Test.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
0D2F0BF125C9068100ABC584 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0D2F0BF325C9068100ABC584 /* AssociatedObject */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = AssociatedObject; sourceTree = BUILT_PRODUCTS_DIR; };
0D2F0BF625C9068100ABC584 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
0D2F0BFE25C906B200ABC584 /* Child.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Child.h; sourceTree = "<group>"; };
0D2F0BFF25C906B200ABC584 /* Child.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Child.m; sourceTree = "<group>"; };
0D2F0C0225C906E500ABC584 /* Child+Test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Child+Test.h"; sourceTree = "<group>"; };
0D2F0C0325C906E500ABC584 /* Child+Test.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "Child+Test.m"; sourceTree = "<group>"; };
0DCB76FD25CD32FC00F9D821 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
0D2F0BF025C9068100ABC584 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
0D2F0BEA25C9068100ABC584 = {
isa = PBXGroup;
children = (
0DCB76FD25CD32FC00F9D821 /* README.md */,
0D2F0BF525C9068100ABC584 /* AssociatedObject */,
0D2F0BF425C9068100ABC584 /* Products */,
);
sourceTree = "<group>";
};
0D2F0BF425C9068100ABC584 /* Products */ = {
isa = PBXGroup;
children = (
0D2F0BF325C9068100ABC584 /* AssociatedObject */,
);
name = Products;
sourceTree = "<group>";
};
0D2F0BF525C9068100ABC584 /* AssociatedObject */ = {
isa = PBXGroup;
children = (
0D2F0BF625C9068100ABC584 /* main.m */,
0D2F0BFE25C906B200ABC584 /* Child.h */,
0D2F0BFF25C906B200ABC584 /* Child.m */,
0D2F0C0225C906E500ABC584 /* Child+Test.h */,
0D2F0C0325C906E500ABC584 /* Child+Test.m */,
);
path = AssociatedObject;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
0D2F0BF225C9068100ABC584 /* AssociatedObject */ = {
isa = PBXNativeTarget;
buildConfigurationList = 0D2F0BFA25C9068100ABC584 /* Build configuration list for PBXNativeTarget "AssociatedObject" */;
buildPhases = (
0D2F0BEF25C9068100ABC584 /* Sources */,
0D2F0BF025C9068100ABC584 /* Frameworks */,
0D2F0BF125C9068100ABC584 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = AssociatedObject;
productName = AssociatedObject;
productReference = 0D2F0BF325C9068100ABC584 /* AssociatedObject */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
0D2F0BEB25C9068100ABC584 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1240;
TargetAttributes = {
0D2F0BF225C9068100ABC584 = {
CreatedOnToolsVersion = 12.4;
};
};
};
buildConfigurationList = 0D2F0BEE25C9068100ABC584 /* Build configuration list for PBXProject "AssociatedObject" */;
compatibilityVersion = "Xcode 9.3";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 0D2F0BEA25C9068100ABC584;
productRefGroup = 0D2F0BF425C9068100ABC584 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
0D2F0BF225C9068100ABC584 /* AssociatedObject */,
);
};
/* End PBXProject section */

/* Begin PBXSourcesBuildPhase section */
0D2F0BEF25C9068100ABC584 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0D2F0C0025C906B200ABC584 /* Child.m in Sources */,
0D2F0BF725C9068100ABC584 /* main.m in Sources */,
0D2F0C0425C906E500ABC584 /* Child+Test.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin XCBuildConfiguration section */
0D2F0BF825C9068100ABC584 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 11.1;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
0D2F0BF925C9068100ABC584 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 11.1;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
};
name = Release;
};
0D2F0BFB25C9068100ABC584 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
0D2F0BFC25C9068100ABC584 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
0D2F0BEE25C9068100ABC584 /* Build configuration list for PBXProject "AssociatedObject" */ = {
isa = XCConfigurationList;
buildConfigurations = (
0D2F0BF825C9068100ABC584 /* Debug */,
0D2F0BF925C9068100ABC584 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
0D2F0BFA25C9068100ABC584 /* Build configuration list for PBXNativeTarget "AssociatedObject" */ = {
isa = XCConfigurationList;
buildConfigurations = (
0D2F0BFB25C9068100ABC584 /* Debug */,
0D2F0BFC25C9068100ABC584 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 0D2F0BEB25C9068100ABC584 /* Project object */;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions AssociatedObject/AssociatedObject/Child+Test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Child+Test.h
// AssociatedObject
//
// Created by pro648 on 2021/2/2.
//

#import "Child.h"

NS_ASSUME_NONNULL_BEGIN

@interface Child (Test)

@property (nonatomic, copy) NSString *name;

@end

NS_ASSUME_NONNULL_END
71 changes: 71 additions & 0 deletions AssociatedObject/AssociatedObject/Child+Test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Child+Test.m
// AssociatedObject
//
// Created by pro648 on 2021/2/2.
//

#import "Child+Test.h"
#import <objc/runtime.h>

@implementation Child (Test)

// 关联对象

// 占4个字节
int age = 0;

// 指针变量,占8个字节。
static const void *vNameKey = &vNameKey;

// 只占一个字节
static const char cNameKey;

- (void)setName:(NSString *)name {
objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC);
// objc_setAssociatedObject(self, &age, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
// objc_setAssociatedObject(self, @"pro648", name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)name {
return objc_getAssociatedObject(self, @selector(name));
// return objc_getAssociatedObject(self, &age);
// return objc_getAssociatedObject(self, @"pro648");
}

/*
// 使用字典存储成员变量的值
// 缺点:添加一个属性,就需要添加一个字典,很麻烦。存在内存泄漏、线程安全问题。
NSMutableDictionary *nameDict;
#define key [NSString stringWithFormat:@"%p", self]
+ (void)initialize {
if (self == Child.class) {
nameDict = [NSMutableDictionary dictionary];
}
}
- (void)setName:(NSString *)name {
nameDict[key] = name;
}
- (NSString *)name {
return nameDict[key];
}
*/

/*
// 使用全局变量存储成员变量的值
// 问题:不能区分不同实例
static NSString *_name;
- (void)setName:(NSString *)name {
_name = name;
}
- (NSString *)name {
return _name;
}
*/

@end
Loading

0 comments on commit 4540370

Please sign in to comment.