├── .gitignore ├── LICENSE ├── README.md ├── RuntimeDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── RuntimeDemo ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json ├── Contents.json └── LaunchImage.launchimage │ └── Contents.json ├── Info.plist ├── NSObject+Runtime.h ├── NSObject+Runtime.m ├── PrefixHeader.pch ├── Safe ├── NSArray+Safe.h ├── NSArray+Safe.m ├── NSDictionary+Safe.h ├── NSDictionary+Safe.m ├── NSNull+Safe.h ├── NSNull+Safe.m ├── NSString+Safe.h └── NSString+Safe.m ├── TestClass+Category.h ├── TestClass+Category.m ├── TestClass.h ├── TestClass.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 lisong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RuntimeDemo 2 | RuntimeDemo 3 | 4 | 详细讲解可以看我的简书文章:[Runtime的运用和减少应用崩溃 5 | ](http://www.jianshu.com/p/35971a7e8bf6)。 6 | -------------------------------------------------------------------------------- /RuntimeDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 9922D80D1ED68B0400F8122C /* NSObject+Runtime.m in Sources */ = {isa = PBXBuildFile; fileRef = 9922D80C1ED68B0400F8122C /* NSObject+Runtime.m */; }; 11 | 99DEF87F1E9C82ED006CD70B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 99DEF87E1E9C82ED006CD70B /* main.m */; }; 12 | 99DEF88A1E9C82ED006CD70B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 99DEF8891E9C82ED006CD70B /* Assets.xcassets */; }; 13 | 99F4008A1EDFE99700FC12D9 /* TestClass.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F400891EDFE99700FC12D9 /* TestClass.m */; }; 14 | 99F4008D1EDFE9D600FC12D9 /* TestClass+Category.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F4008C1EDFE9D600FC12D9 /* TestClass+Category.m */; }; 15 | 99F400BB1EE01F7E00FC12D9 /* NSArray+Safe.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F400AC1EE01F7E00FC12D9 /* NSArray+Safe.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; 16 | 99F400BC1EE01F7E00FC12D9 /* NSDictionary+Safe.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F400AE1EE01F7E00FC12D9 /* NSDictionary+Safe.m */; }; 17 | 99F400C01EE01F7E00FC12D9 /* NSNull+Safe.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F400B61EE01F7E00FC12D9 /* NSNull+Safe.m */; }; 18 | 99F400C21EE01F7E00FC12D9 /* NSString+Safe.m in Sources */ = {isa = PBXBuildFile; fileRef = 99F400BA1EE01F7E00FC12D9 /* NSString+Safe.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXFileReference section */ 22 | 9922D80B1ED68B0300F8122C /* NSObject+Runtime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+Runtime.h"; sourceTree = ""; }; 23 | 9922D80C1ED68B0400F8122C /* NSObject+Runtime.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+Runtime.m"; sourceTree = ""; }; 24 | 9922D80E1ED6B4F100F8122C /* PrefixHeader.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = ""; }; 25 | 99DEF87A1E9C82ED006CD70B /* RuntimeDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RuntimeDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 26 | 99DEF87E1E9C82ED006CD70B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 27 | 99DEF8891E9C82ED006CD70B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 99F400881EDFE99700FC12D9 /* TestClass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestClass.h; sourceTree = ""; }; 29 | 99F400891EDFE99700FC12D9 /* TestClass.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestClass.m; sourceTree = ""; }; 30 | 99F4008B1EDFE9D600FC12D9 /* TestClass+Category.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "TestClass+Category.h"; sourceTree = ""; }; 31 | 99F4008C1EDFE9D600FC12D9 /* TestClass+Category.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "TestClass+Category.m"; sourceTree = ""; }; 32 | 99F400AB1EE01F7E00FC12D9 /* NSArray+Safe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Safe.h"; sourceTree = ""; }; 33 | 99F400AC1EE01F7E00FC12D9 /* NSArray+Safe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Safe.m"; sourceTree = ""; }; 34 | 99F400AD1EE01F7E00FC12D9 /* NSDictionary+Safe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+Safe.h"; sourceTree = ""; }; 35 | 99F400AE1EE01F7E00FC12D9 /* NSDictionary+Safe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+Safe.m"; sourceTree = ""; }; 36 | 99F400B51EE01F7E00FC12D9 /* NSNull+Safe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNull+Safe.h"; sourceTree = ""; }; 37 | 99F400B61EE01F7E00FC12D9 /* NSNull+Safe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSNull+Safe.m"; sourceTree = ""; }; 38 | 99F400B91EE01F7E00FC12D9 /* NSString+Safe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+Safe.h"; sourceTree = ""; }; 39 | 99F400BA1EE01F7E00FC12D9 /* NSString+Safe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+Safe.m"; sourceTree = ""; }; 40 | /* End PBXFileReference section */ 41 | 42 | /* Begin PBXFrameworksBuildPhase section */ 43 | 99DEF8771E9C82ED006CD70B /* Frameworks */ = { 44 | isa = PBXFrameworksBuildPhase; 45 | buildActionMask = 2147483647; 46 | files = ( 47 | ); 48 | runOnlyForDeploymentPostprocessing = 0; 49 | }; 50 | /* End PBXFrameworksBuildPhase section */ 51 | 52 | /* Begin PBXGroup section */ 53 | 99DEF8711E9C82ED006CD70B = { 54 | isa = PBXGroup; 55 | children = ( 56 | 99DEF87C1E9C82ED006CD70B /* RuntimeDemo */, 57 | 99DEF87B1E9C82ED006CD70B /* Products */, 58 | ); 59 | sourceTree = ""; 60 | }; 61 | 99DEF87B1E9C82ED006CD70B /* Products */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 99DEF87A1E9C82ED006CD70B /* RuntimeDemo.app */, 65 | ); 66 | name = Products; 67 | sourceTree = ""; 68 | }; 69 | 99DEF87C1E9C82ED006CD70B /* RuntimeDemo */ = { 70 | isa = PBXGroup; 71 | children = ( 72 | 99F400AA1EE01F7E00FC12D9 /* Safe */, 73 | 99F400881EDFE99700FC12D9 /* TestClass.h */, 74 | 99F400891EDFE99700FC12D9 /* TestClass.m */, 75 | 99F4008B1EDFE9D600FC12D9 /* TestClass+Category.h */, 76 | 99F4008C1EDFE9D600FC12D9 /* TestClass+Category.m */, 77 | 9922D80B1ED68B0300F8122C /* NSObject+Runtime.h */, 78 | 9922D80C1ED68B0400F8122C /* NSObject+Runtime.m */, 79 | 9922D80E1ED6B4F100F8122C /* PrefixHeader.pch */, 80 | 99DEF8891E9C82ED006CD70B /* Assets.xcassets */, 81 | 99DEF87D1E9C82ED006CD70B /* Supporting Files */, 82 | ); 83 | path = RuntimeDemo; 84 | sourceTree = ""; 85 | }; 86 | 99DEF87D1E9C82ED006CD70B /* Supporting Files */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 99DEF87E1E9C82ED006CD70B /* main.m */, 90 | ); 91 | name = "Supporting Files"; 92 | sourceTree = ""; 93 | }; 94 | 99F400AA1EE01F7E00FC12D9 /* Safe */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 99F400AB1EE01F7E00FC12D9 /* NSArray+Safe.h */, 98 | 99F400AC1EE01F7E00FC12D9 /* NSArray+Safe.m */, 99 | 99F400AD1EE01F7E00FC12D9 /* NSDictionary+Safe.h */, 100 | 99F400AE1EE01F7E00FC12D9 /* NSDictionary+Safe.m */, 101 | 99F400B91EE01F7E00FC12D9 /* NSString+Safe.h */, 102 | 99F400BA1EE01F7E00FC12D9 /* NSString+Safe.m */, 103 | 99F400B51EE01F7E00FC12D9 /* NSNull+Safe.h */, 104 | 99F400B61EE01F7E00FC12D9 /* NSNull+Safe.m */, 105 | ); 106 | path = Safe; 107 | sourceTree = ""; 108 | }; 109 | /* End PBXGroup section */ 110 | 111 | /* Begin PBXNativeTarget section */ 112 | 99DEF8791E9C82ED006CD70B /* RuntimeDemo */ = { 113 | isa = PBXNativeTarget; 114 | buildConfigurationList = 99DEF8911E9C82ED006CD70B /* Build configuration list for PBXNativeTarget "RuntimeDemo" */; 115 | buildPhases = ( 116 | 99DEF8761E9C82ED006CD70B /* Sources */, 117 | 99DEF8771E9C82ED006CD70B /* Frameworks */, 118 | 99DEF8781E9C82ED006CD70B /* Resources */, 119 | ); 120 | buildRules = ( 121 | ); 122 | dependencies = ( 123 | ); 124 | name = RuntimeDemo; 125 | productName = RuntimeDemo; 126 | productReference = 99DEF87A1E9C82ED006CD70B /* RuntimeDemo.app */; 127 | productType = "com.apple.product-type.application"; 128 | }; 129 | /* End PBXNativeTarget section */ 130 | 131 | /* Begin PBXProject section */ 132 | 99DEF8721E9C82ED006CD70B /* Project object */ = { 133 | isa = PBXProject; 134 | attributes = { 135 | LastUpgradeCheck = 0830; 136 | ORGANIZATIONNAME = lisong; 137 | TargetAttributes = { 138 | 99DEF8791E9C82ED006CD70B = { 139 | CreatedOnToolsVersion = 8.3.1; 140 | ProvisioningStyle = Automatic; 141 | }; 142 | }; 143 | }; 144 | buildConfigurationList = 99DEF8751E9C82ED006CD70B /* Build configuration list for PBXProject "RuntimeDemo" */; 145 | compatibilityVersion = "Xcode 3.2"; 146 | developmentRegion = English; 147 | hasScannedForEncodings = 0; 148 | knownRegions = ( 149 | en, 150 | Base, 151 | ); 152 | mainGroup = 99DEF8711E9C82ED006CD70B; 153 | productRefGroup = 99DEF87B1E9C82ED006CD70B /* Products */; 154 | projectDirPath = ""; 155 | projectRoot = ""; 156 | targets = ( 157 | 99DEF8791E9C82ED006CD70B /* RuntimeDemo */, 158 | ); 159 | }; 160 | /* End PBXProject section */ 161 | 162 | /* Begin PBXResourcesBuildPhase section */ 163 | 99DEF8781E9C82ED006CD70B /* Resources */ = { 164 | isa = PBXResourcesBuildPhase; 165 | buildActionMask = 2147483647; 166 | files = ( 167 | 99DEF88A1E9C82ED006CD70B /* Assets.xcassets in Resources */, 168 | ); 169 | runOnlyForDeploymentPostprocessing = 0; 170 | }; 171 | /* End PBXResourcesBuildPhase section */ 172 | 173 | /* Begin PBXSourcesBuildPhase section */ 174 | 99DEF8761E9C82ED006CD70B /* Sources */ = { 175 | isa = PBXSourcesBuildPhase; 176 | buildActionMask = 2147483647; 177 | files = ( 178 | 99F400C01EE01F7E00FC12D9 /* NSNull+Safe.m in Sources */, 179 | 99F4008A1EDFE99700FC12D9 /* TestClass.m in Sources */, 180 | 99F400C21EE01F7E00FC12D9 /* NSString+Safe.m in Sources */, 181 | 99F400BB1EE01F7E00FC12D9 /* NSArray+Safe.m in Sources */, 182 | 9922D80D1ED68B0400F8122C /* NSObject+Runtime.m in Sources */, 183 | 99F4008D1EDFE9D600FC12D9 /* TestClass+Category.m in Sources */, 184 | 99DEF87F1E9C82ED006CD70B /* main.m in Sources */, 185 | 99F400BC1EE01F7E00FC12D9 /* NSDictionary+Safe.m in Sources */, 186 | ); 187 | runOnlyForDeploymentPostprocessing = 0; 188 | }; 189 | /* End PBXSourcesBuildPhase section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | 99DEF88F1E9C82ED006CD70B /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_ANALYZER_NONNULL = YES; 197 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 198 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 199 | CLANG_CXX_LIBRARY = "libc++"; 200 | CLANG_ENABLE_MODULES = YES; 201 | CLANG_ENABLE_OBJC_ARC = YES; 202 | CLANG_WARN_BOOL_CONVERSION = YES; 203 | CLANG_WARN_CONSTANT_CONVERSION = YES; 204 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 205 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 206 | CLANG_WARN_EMPTY_BODY = YES; 207 | CLANG_WARN_ENUM_CONVERSION = YES; 208 | CLANG_WARN_INFINITE_RECURSION = YES; 209 | CLANG_WARN_INT_CONVERSION = YES; 210 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 211 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 212 | CLANG_WARN_UNREACHABLE_CODE = YES; 213 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 214 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 215 | COPY_PHASE_STRIP = NO; 216 | DEBUG_INFORMATION_FORMAT = dwarf; 217 | ENABLE_STRICT_OBJC_MSGSEND = YES; 218 | ENABLE_TESTABILITY = YES; 219 | GCC_C_LANGUAGE_STANDARD = gnu99; 220 | GCC_DYNAMIC_NO_PIC = NO; 221 | GCC_NO_COMMON_BLOCKS = YES; 222 | GCC_OPTIMIZATION_LEVEL = 0; 223 | GCC_PREPROCESSOR_DEFINITIONS = ( 224 | "DEBUG=1", 225 | "$(inherited)", 226 | ); 227 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 228 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 229 | GCC_WARN_UNDECLARED_SELECTOR = YES; 230 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 231 | GCC_WARN_UNUSED_FUNCTION = YES; 232 | GCC_WARN_UNUSED_VARIABLE = YES; 233 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 234 | MTL_ENABLE_DEBUG_INFO = YES; 235 | ONLY_ACTIVE_ARCH = YES; 236 | SDKROOT = iphoneos; 237 | TARGETED_DEVICE_FAMILY = "1,2"; 238 | }; 239 | name = Debug; 240 | }; 241 | 99DEF8901E9C82ED006CD70B /* Release */ = { 242 | isa = XCBuildConfiguration; 243 | buildSettings = { 244 | ALWAYS_SEARCH_USER_PATHS = NO; 245 | CLANG_ANALYZER_NONNULL = YES; 246 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_WARN_BOOL_CONVERSION = YES; 252 | CLANG_WARN_CONSTANT_CONVERSION = YES; 253 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 254 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 255 | CLANG_WARN_EMPTY_BODY = YES; 256 | CLANG_WARN_ENUM_CONVERSION = YES; 257 | CLANG_WARN_INFINITE_RECURSION = YES; 258 | CLANG_WARN_INT_CONVERSION = YES; 259 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 260 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 261 | CLANG_WARN_UNREACHABLE_CODE = YES; 262 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 263 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 264 | COPY_PHASE_STRIP = NO; 265 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 266 | ENABLE_NS_ASSERTIONS = NO; 267 | ENABLE_STRICT_OBJC_MSGSEND = YES; 268 | GCC_C_LANGUAGE_STANDARD = gnu99; 269 | GCC_NO_COMMON_BLOCKS = YES; 270 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 271 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 272 | GCC_WARN_UNDECLARED_SELECTOR = YES; 273 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 274 | GCC_WARN_UNUSED_FUNCTION = YES; 275 | GCC_WARN_UNUSED_VARIABLE = YES; 276 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 277 | MTL_ENABLE_DEBUG_INFO = NO; 278 | SDKROOT = iphoneos; 279 | TARGETED_DEVICE_FAMILY = "1,2"; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Release; 283 | }; 284 | 99DEF8921E9C82ED006CD70B /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | GCC_PREFIX_HEADER = "$(SRCROOT)/RuntimeDemo/PrefixHeader.pch"; 289 | INFOPLIST_FILE = RuntimeDemo/Info.plist; 290 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 291 | PRODUCT_BUNDLE_IDENTIFIER = com.lisong.RuntimeDemo; 292 | PRODUCT_NAME = "$(TARGET_NAME)"; 293 | }; 294 | name = Debug; 295 | }; 296 | 99DEF8931E9C82ED006CD70B /* Release */ = { 297 | isa = XCBuildConfiguration; 298 | buildSettings = { 299 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 300 | GCC_PREFIX_HEADER = "$(SRCROOT)/RuntimeDemo/PrefixHeader.pch"; 301 | INFOPLIST_FILE = RuntimeDemo/Info.plist; 302 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 303 | PRODUCT_BUNDLE_IDENTIFIER = com.lisong.RuntimeDemo; 304 | PRODUCT_NAME = "$(TARGET_NAME)"; 305 | }; 306 | name = Release; 307 | }; 308 | /* End XCBuildConfiguration section */ 309 | 310 | /* Begin XCConfigurationList section */ 311 | 99DEF8751E9C82ED006CD70B /* Build configuration list for PBXProject "RuntimeDemo" */ = { 312 | isa = XCConfigurationList; 313 | buildConfigurations = ( 314 | 99DEF88F1E9C82ED006CD70B /* Debug */, 315 | 99DEF8901E9C82ED006CD70B /* Release */, 316 | ); 317 | defaultConfigurationIsVisible = 0; 318 | defaultConfigurationName = Release; 319 | }; 320 | 99DEF8911E9C82ED006CD70B /* Build configuration list for PBXNativeTarget "RuntimeDemo" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | 99DEF8921E9C82ED006CD70B /* Debug */, 324 | 99DEF8931E9C82ED006CD70B /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | /* End XCConfigurationList section */ 330 | }; 331 | rootObject = 99DEF8721E9C82ED006CD70B /* Project object */; 332 | } 333 | -------------------------------------------------------------------------------- /RuntimeDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RuntimeDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /RuntimeDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /RuntimeDemo/Assets.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "scale" : "1x" 8 | }, 9 | { 10 | "orientation" : "portrait", 11 | "idiom" : "iphone", 12 | "extent" : "full-screen", 13 | "scale" : "2x" 14 | }, 15 | { 16 | "orientation" : "portrait", 17 | "idiom" : "iphone", 18 | "extent" : "full-screen", 19 | "subtype" : "retina4", 20 | "scale" : "2x" 21 | }, 22 | { 23 | "orientation" : "portrait", 24 | "idiom" : "ipad", 25 | "extent" : "to-status-bar", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "orientation" : "portrait", 30 | "idiom" : "ipad", 31 | "extent" : "full-screen", 32 | "scale" : "1x" 33 | }, 34 | { 35 | "orientation" : "landscape", 36 | "idiom" : "ipad", 37 | "extent" : "to-status-bar", 38 | "scale" : "1x" 39 | }, 40 | { 41 | "orientation" : "landscape", 42 | "idiom" : "ipad", 43 | "extent" : "full-screen", 44 | "scale" : "1x" 45 | }, 46 | { 47 | "orientation" : "portrait", 48 | "idiom" : "ipad", 49 | "extent" : "to-status-bar", 50 | "scale" : "2x" 51 | }, 52 | { 53 | "orientation" : "portrait", 54 | "idiom" : "ipad", 55 | "extent" : "full-screen", 56 | "scale" : "2x" 57 | }, 58 | { 59 | "orientation" : "landscape", 60 | "idiom" : "ipad", 61 | "extent" : "to-status-bar", 62 | "scale" : "2x" 63 | }, 64 | { 65 | "orientation" : "landscape", 66 | "idiom" : "ipad", 67 | "extent" : "full-screen", 68 | "scale" : "2x" 69 | }, 70 | { 71 | "orientation" : "portrait", 72 | "idiom" : "iphone", 73 | "extent" : "full-screen", 74 | "minimum-system-version" : "8.0", 75 | "subtype" : "736h", 76 | "scale" : "3x" 77 | }, 78 | { 79 | "orientation" : "landscape", 80 | "idiom" : "iphone", 81 | "extent" : "full-screen", 82 | "minimum-system-version" : "8.0", 83 | "subtype" : "736h", 84 | "scale" : "3x" 85 | }, 86 | { 87 | "orientation" : "portrait", 88 | "idiom" : "iphone", 89 | "extent" : "full-screen", 90 | "minimum-system-version" : "8.0", 91 | "subtype" : "667h", 92 | "scale" : "2x" 93 | }, 94 | { 95 | "orientation" : "portrait", 96 | "idiom" : "iphone", 97 | "extent" : "full-screen", 98 | "minimum-system-version" : "7.0", 99 | "scale" : "2x" 100 | }, 101 | { 102 | "orientation" : "portrait", 103 | "idiom" : "iphone", 104 | "extent" : "full-screen", 105 | "minimum-system-version" : "7.0", 106 | "subtype" : "retina4", 107 | "scale" : "2x" 108 | }, 109 | { 110 | "orientation" : "portrait", 111 | "idiom" : "ipad", 112 | "extent" : "full-screen", 113 | "minimum-system-version" : "7.0", 114 | "scale" : "1x" 115 | }, 116 | { 117 | "orientation" : "landscape", 118 | "idiom" : "ipad", 119 | "extent" : "full-screen", 120 | "minimum-system-version" : "7.0", 121 | "scale" : "1x" 122 | }, 123 | { 124 | "orientation" : "portrait", 125 | "idiom" : "ipad", 126 | "extent" : "full-screen", 127 | "minimum-system-version" : "7.0", 128 | "scale" : "2x" 129 | }, 130 | { 131 | "orientation" : "landscape", 132 | "idiom" : "ipad", 133 | "extent" : "full-screen", 134 | "minimum-system-version" : "7.0", 135 | "scale" : "2x" 136 | } 137 | ], 138 | "info" : { 139 | "version" : 1, 140 | "author" : "xcode" 141 | } 142 | } -------------------------------------------------------------------------------- /RuntimeDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchImage 25 | UIRequiredDeviceCapabilities 26 | 27 | armv7 28 | 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /RuntimeDemo/NSObject+Runtime.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+Runtime.h 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/5/25. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSObject (Runtime) 12 | 13 | /** 获取成员变量,包括属性生成的成员变量 */ 14 | + (NSArray *)fetchIvarList; 15 | 16 | /** 获取类的属性列表,包括私有和公有属性,也包括分类中的属性 */ 17 | + (NSArray *)fetchPropertyList; 18 | 19 | /** 获取对象方法列表:包括getter, setter, 分类中的方法等 */ 20 | + (NSArray *)fetchInstanceMethodList; 21 | 22 | /** 获取类方法列表 包括分类里面的 */ 23 | + (NSArray *)fetchClassMethodList; 24 | 25 | /** 获取协议列表,包括.h .m 和分类里的 */ 26 | + (NSArray *)fetchProtocolList; 27 | 28 | /** 添加一个方法 */ 29 | + (void)addMethod:(SEL)methodSel methodImp:(SEL)methodImp; 30 | 31 | /** 实例方法交换 */ 32 | + (void)swapMethod:(SEL)originMethod currentMethod:(SEL)currentMethod; 33 | 34 | /** 类方法交换 */ 35 | + (void)swapClassMethod:(SEL)originMethod currentMethod:(SEL)currentMethod; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /RuntimeDemo/NSObject+Runtime.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+Runtime.m 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/5/25. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "NSObject+Runtime.h" 10 | #import 11 | 12 | @implementation NSObject (Runtime) 13 | 14 | + (NSArray *)fetchIvarList 15 | { 16 | unsigned int count = 0; 17 | Ivar *ivarList = class_copyIvarList(self, &count); 18 | 19 | NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; 20 | for (unsigned int i = 0; i < count; i++) 21 | { 22 | NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2]; 23 | const char *ivarName = ivar_getName(ivarList[i]); 24 | const char *ivarType = ivar_getTypeEncoding(ivarList[i]); 25 | dic[@"type"] = [NSString stringWithUTF8String:ivarType]; 26 | dic[@"ivarName"] = [NSString stringWithUTF8String:ivarName]; 27 | 28 | [mutableList addObject:dic]; 29 | } 30 | free(ivarList); 31 | return [NSArray arrayWithArray:mutableList]; 32 | } 33 | 34 | + (NSArray *)fetchPropertyList 35 | { 36 | unsigned int count = 0; 37 | objc_property_t *propertyList = class_copyPropertyList(self, &count); 38 | 39 | NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; 40 | for (unsigned int i = 0; i < count; i++) 41 | { 42 | const char *propertyName = property_getAttributes(propertyList[i]); 43 | [mutableList addObject:[NSString stringWithUTF8String:propertyName]]; 44 | } 45 | free(propertyList); 46 | return [NSArray arrayWithArray:mutableList]; 47 | } 48 | 49 | + (NSArray *)fetchInstanceMethodList 50 | { 51 | unsigned int count = 0; 52 | Method *methodList = class_copyMethodList(self, &count); 53 | 54 | NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; 55 | for (unsigned int i = 0; i < count; i++) 56 | { 57 | Method method = methodList[i]; 58 | SEL methodName = method_getName(method); 59 | [mutableList addObject:NSStringFromSelector(methodName)]; 60 | } 61 | free(methodList); 62 | return [NSArray arrayWithArray:mutableList]; 63 | } 64 | 65 | + (NSArray *)fetchClassMethodList 66 | { 67 | unsigned int count = 0; 68 | Method *methodList = class_copyMethodList(object_getClass(self), &count); 69 | 70 | NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; 71 | for (unsigned int i = 0; i < count; i++) 72 | { 73 | Method method = methodList[i]; 74 | SEL methodName = method_getName(method); 75 | [mutableList addObject:NSStringFromSelector(methodName)]; 76 | } 77 | free(methodList); 78 | return [NSArray arrayWithArray:mutableList]; 79 | } 80 | 81 | + (NSArray *)fetchProtocolList 82 | { 83 | unsigned int count = 0; 84 | __unsafe_unretained Protocol **protocolList = class_copyProtocolList(self, &count); 85 | 86 | NSMutableArray *mutableList = [NSMutableArray arrayWithCapacity:count]; 87 | for (unsigned int i = 0; i < count; i++ ) 88 | { 89 | Protocol *protocol = protocolList[i]; 90 | const char *protocolName = protocol_getName(protocol); 91 | [mutableList addObject:[NSString stringWithUTF8String:protocolName]]; 92 | } 93 | 94 | return [NSArray arrayWithArray:mutableList]; 95 | } 96 | 97 | + (void)addMethod:(SEL)methodSel methodImp:(SEL)methodImp; 98 | { 99 | Method method = class_getInstanceMethod(self, methodImp); 100 | IMP methodIMP = method_getImplementation(method); 101 | const char *types = method_getTypeEncoding(method); 102 | class_addMethod(self, methodSel, methodIMP, types); 103 | } 104 | 105 | + (void)swapMethod:(SEL)originMethod currentMethod:(SEL)currentMethod; 106 | { 107 | Method firstMethod = class_getInstanceMethod(self, originMethod); 108 | Method secondMethod = class_getInstanceMethod(self, currentMethod); 109 | method_exchangeImplementations(firstMethod, secondMethod); 110 | } 111 | 112 | + (void)swapClassMethod:(SEL)originMethod currentMethod:(SEL)currentMethod; 113 | { 114 | Method firstMethod = class_getClassMethod(self, originMethod); 115 | Method secondMethod = class_getClassMethod(self, currentMethod); 116 | method_exchangeImplementations(firstMethod, secondMethod); 117 | } 118 | 119 | @end 120 | -------------------------------------------------------------------------------- /RuntimeDemo/PrefixHeader.pch: -------------------------------------------------------------------------------- 1 | // 2 | // PrefixHeader.pch 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/5/25. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #ifndef PrefixHeader_pch 10 | #define PrefixHeader_pch 11 | 12 | // Include any system framework and library headers here that should be included in all compilation units. 13 | // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. 14 | 15 | #import 16 | #import "NSObject+Runtime.h" 17 | 18 | #endif /* PrefixHeader_pch */ 19 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSArray+Safe.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Safe.h 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSArray (Safe) 12 | 13 | @end 14 | 15 | @interface NSMutableArray (Safe) 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSArray+Safe.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Safe.m 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "NSArray+Safe.h" 10 | 11 | @implementation NSArray (Safe) 12 | 13 | + (void)load 14 | { 15 | [NSClassFromString(@"__NSPlaceholderArray") swapMethod:@selector(initWithObjects:count:) 16 | currentMethod:@selector(ls_initWithObjects:count:)]; 17 | 18 | [NSClassFromString(@"__NSArrayI") swapMethod:@selector(objectAtIndex:) 19 | currentMethod:@selector(ls_objectAtIndex:)]; 20 | 21 | [NSClassFromString(@"__NSArray0") swapMethod:@selector(objectAtIndex:) 22 | currentMethod:@selector(ls_zeroObjectAtIndex:)]; 23 | 24 | [NSClassFromString(@"__NSSingleObjectArrayI") swapMethod:@selector(objectAtIndex:) 25 | currentMethod:@selector(ls_singleObjectAtIndex:)]; 26 | } 27 | 28 | - (instancetype)ls_initWithObjects:(id *)objects count:(NSUInteger)cnt 29 | { 30 | NSUInteger newCnt = 0; 31 | for (NSUInteger i = 0; i < cnt; i++) 32 | { 33 | if (!objects[i]) 34 | { 35 | break; 36 | } 37 | newCnt++; 38 | } 39 | 40 | return [self ls_initWithObjects:objects count:newCnt]; 41 | } 42 | 43 | - (id)ls_objectAtIndex:(NSUInteger)index 44 | { 45 | if (index >= [self count]) 46 | { 47 | return nil; 48 | } 49 | return [self ls_objectAtIndex:index]; 50 | } 51 | 52 | - (id)ls_zeroObjectAtIndex:(NSUInteger)index 53 | { 54 | if (index >= self.count) 55 | { 56 | return nil; 57 | } 58 | return [self ls_zeroObjectAtIndex:index]; 59 | } 60 | 61 | - (id)ls_singleObjectAtIndex:(NSUInteger)index 62 | { 63 | if (index >= self.count) 64 | { 65 | return nil; 66 | } 67 | return [self ls_singleObjectAtIndex:index]; 68 | } 69 | 70 | @end 71 | 72 | 73 | 74 | @implementation NSMutableArray (Safe) 75 | 76 | + (void)load 77 | { 78 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(objectAtIndex:) 79 | currentMethod:@selector(ls_objectAtIndex:)]; 80 | 81 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(addObject:) 82 | currentMethod:@selector(ls_addObject:)]; 83 | 84 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(removeObjectAtIndex:) 85 | currentMethod:@selector(ls_removeObjectAtIndex:)]; 86 | 87 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(replaceObjectAtIndex:withObject:) 88 | currentMethod:@selector(ls_replaceObjectAtIndex:withObject:)]; 89 | 90 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(removeObjectsInRange:) 91 | currentMethod:@selector(ls_removeObjectsInRange:)]; 92 | 93 | [NSClassFromString(@"__NSArrayM") swapMethod:@selector(insertObject:atIndex:) 94 | currentMethod:@selector(ls_insertObject:atIndex:)]; 95 | } 96 | 97 | - (id)ls_objectAtIndex:(NSUInteger)index 98 | { 99 | if (index >= self.count) 100 | { 101 | return nil; 102 | } 103 | return [self ls_objectAtIndex:index]; 104 | } 105 | 106 | - (void)ls_addObject:(id)anObject 107 | { 108 | if (!anObject) 109 | { 110 | return; 111 | } 112 | [self ls_addObject:anObject]; 113 | } 114 | 115 | - (void)ls_removeObjectAtIndex:(NSUInteger)index 116 | { 117 | if (index >= [self count]) 118 | { 119 | return; 120 | } 121 | 122 | return [self ls_removeObjectAtIndex:index]; 123 | } 124 | 125 | - (void)ls_replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject 126 | { 127 | if (index >= [self count]) 128 | { 129 | return; 130 | } 131 | 132 | if (!anObject) 133 | { 134 | return; 135 | } 136 | 137 | [self ls_replaceObjectAtIndex:index withObject:anObject]; 138 | } 139 | 140 | - (void)ls_removeObjectsInRange:(NSRange)range 141 | { 142 | if (range.location > self.count) 143 | { 144 | return; 145 | } 146 | 147 | if (range.length > self.count) 148 | { 149 | return; 150 | } 151 | 152 | if ((range.location + range.length) > self.count) 153 | { 154 | return; 155 | } 156 | 157 | return [self ls_removeObjectsInRange:range]; 158 | } 159 | 160 | - (void)ls_insertObject:(id)anObject atIndex:(NSUInteger)index 161 | { 162 | if (index > self.count) 163 | { 164 | return; 165 | } 166 | 167 | if (!anObject) 168 | { 169 | return; 170 | } 171 | 172 | [self ls_insertObject:anObject atIndex:index]; 173 | } 174 | 175 | @end 176 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSDictionary+Safe.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionary+Safe.h 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSDictionary (Safe) 12 | 13 | @end 14 | 15 | @interface NSMutableDictionary (Safe) 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSDictionary+Safe.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionary+Safe.m 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "NSDictionary+Safe.h" 10 | 11 | @implementation NSDictionary (Safe) 12 | 13 | + (void)load 14 | { 15 | [NSClassFromString(@"__NSPlaceholderDictionary") swapMethod:@selector(initWithObjects:forKeys:count:) 16 | currentMethod:@selector(ls_initWithObjects:forKeys:count:)]; 17 | } 18 | 19 | - (instancetype)ls_initWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt 20 | { 21 | id safeObjects[cnt]; 22 | id safeKeys[cnt]; 23 | NSUInteger j = 0; 24 | for (NSUInteger i = 0; i < cnt; i++) 25 | { 26 | id key = keys[i]; 27 | id obj = objects[i]; 28 | if (!key) 29 | { 30 | continue; 31 | } 32 | if (!obj) 33 | { 34 | obj = [NSNull null]; 35 | } 36 | safeKeys[j] = key; 37 | safeObjects[j] = obj; 38 | j++; 39 | } 40 | 41 | return [self ls_initWithObjects:safeObjects forKeys:safeKeys count:j]; 42 | } 43 | 44 | @end 45 | 46 | 47 | @implementation NSMutableDictionary (Safe) 48 | 49 | + (void)load 50 | { 51 | [NSClassFromString(@"__NSDictionaryM") swapMethod:@selector(setObject:forKey:) 52 | currentMethod:@selector(ls_setObject:forKey:)]; 53 | } 54 | 55 | - (void)ls_setObject:(id)anObject forKey:(id )aKey 56 | { 57 | if (!anObject) 58 | { 59 | return; 60 | } 61 | if (!aKey) 62 | { 63 | return; 64 | } 65 | [self ls_setObject:anObject forKey:aKey]; 66 | } 67 | 68 | @end 69 | 70 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSNull+Safe.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSNull+Safe.h 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/11. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSNull (Safe) 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSNull+Safe.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSNull+Safe.m 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/11. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "NSNull+Safe.h" 10 | 11 | @implementation NSNull (Safe) 12 | 13 | + (void)load 14 | { 15 | [NSNull swapMethod:@selector(methodSignatureForSelector:) 16 | currentMethod:@selector(ls_methodSignatureForSelector:)]; 17 | 18 | [NSNull swapMethod:@selector(forwardInvocation:) 19 | currentMethod:@selector(ls_forwardInvocation:)]; 20 | } 21 | 22 | - (NSMethodSignature *)ls_methodSignatureForSelector:(SEL)aSelector 23 | { 24 | NSMethodSignature *sig = [self ls_methodSignatureForSelector:aSelector]; 25 | if (sig) 26 | { 27 | return sig; 28 | } 29 | return [NSMethodSignature signatureWithObjCTypes:@encode(void)]; 30 | } 31 | 32 | - (void)ls_forwardInvocation:(NSInvocation *)anInvocation 33 | { 34 | NSUInteger returnLength = [[anInvocation methodSignature] methodReturnLength]; 35 | if (!returnLength) 36 | { 37 | return; 38 | } 39 | 40 | char buffer[returnLength]; 41 | memset(buffer, 0, returnLength); 42 | 43 | [anInvocation setReturnValue:buffer]; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSString+Safe.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Safe.h 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSString (Safe) 12 | 13 | @end 14 | 15 | @interface NSMutableString (Safe) 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /RuntimeDemo/Safe/NSString+Safe.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Safe.m 3 | // lisong 4 | // 5 | // Created by lisong on 2017/4/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "NSString+Safe.h" 10 | 11 | @implementation NSString (Safe) 12 | 13 | + (void)load 14 | { 15 | [NSClassFromString(@"__NSCFConstantString") swapMethod:@selector(substringFromIndex:) 16 | currentMethod:@selector(lsConstant_substringFromIndex:)]; 17 | 18 | [NSClassFromString(@"NSTaggedPointerString") swapMethod:@selector(substringFromIndex:) 19 | currentMethod:@selector(lsPoint_substringFromIndex:)]; 20 | 21 | [NSClassFromString(@"__NSCFConstantString") swapMethod:@selector(substringToIndex:) 22 | currentMethod:@selector(lsConstant_substringToIndex:)]; 23 | 24 | [NSClassFromString(@"NSTaggedPointerString") swapMethod:@selector(substringToIndex:) 25 | currentMethod:@selector(lsPoint_substringToIndex:)]; 26 | 27 | [NSClassFromString(@"__NSCFConstantString") swapMethod:@selector(substringWithRange:) 28 | currentMethod:@selector(lsConstant_substringWithRange:)]; 29 | 30 | [NSClassFromString(@"NSTaggedPointerString") swapMethod:@selector(substringWithRange:) 31 | currentMethod:@selector(lsPoint_substringWithRange:)]; 32 | } 33 | 34 | - (NSString *)lsConstant_substringFromIndex:(NSUInteger)from 35 | { 36 | if (from > self.length ) 37 | { 38 | return nil; 39 | } 40 | return [self lsConstant_substringFromIndex:from]; 41 | } 42 | 43 | - (NSString *)lsPoint_substringFromIndex:(NSUInteger)from 44 | { 45 | if (from > self.length ) 46 | { 47 | return nil; 48 | } 49 | return [self lsPoint_substringFromIndex:from]; 50 | } 51 | 52 | - (NSString *)lsConstant_substringToIndex:(NSUInteger)to 53 | { 54 | if (to > self.length ) 55 | { 56 | return nil; 57 | } 58 | return [self lsConstant_substringToIndex:to]; 59 | } 60 | 61 | - (NSString *)lsPoint_substringToIndex:(NSUInteger)to 62 | { 63 | if (to > self.length ) 64 | { 65 | return nil; 66 | } 67 | return [self lsPoint_substringToIndex:to]; 68 | } 69 | 70 | - (NSString *)lsConstant_substringWithRange:(NSRange)range 71 | { 72 | if (range.location > self.length) 73 | { 74 | return nil; 75 | } 76 | 77 | if (range.length > self.length) 78 | { 79 | return nil; 80 | } 81 | 82 | if ((range.location + range.length) > self.length) 83 | { 84 | return nil; 85 | } 86 | return [self lsConstant_substringWithRange:range]; 87 | } 88 | 89 | - (NSString *)lsPoint_substringWithRange:(NSRange)range 90 | { 91 | if (range.location > self.length) 92 | { 93 | return nil; 94 | } 95 | 96 | if (range.length > self.length) 97 | { 98 | return nil; 99 | } 100 | 101 | if ((range.location + range.length) > self.length) 102 | { 103 | return nil; 104 | } 105 | return [self lsPoint_substringWithRange:range]; 106 | } 107 | 108 | @end 109 | 110 | 111 | 112 | @implementation NSMutableString (Safe) 113 | 114 | + (void)load 115 | { 116 | [NSClassFromString(@"__NSCFString") swapMethod:@selector(substringFromIndex:) 117 | currentMethod:@selector(ls_substringFromIndex:)]; 118 | 119 | [NSClassFromString(@"__NSCFString") swapMethod:@selector(substringToIndex:) 120 | currentMethod:@selector(ls_substringToIndex:)]; 121 | 122 | [NSClassFromString(@"__NSCFString") swapMethod:@selector(substringWithRange:) 123 | currentMethod:@selector(ls_substringWithRange:)]; 124 | } 125 | 126 | - (NSString *)ls_substringFromIndex:(NSUInteger)from 127 | { 128 | if (from > self.length ) 129 | { 130 | return nil; 131 | } 132 | return [self ls_substringFromIndex:from]; 133 | } 134 | 135 | - (NSString *)ls_substringToIndex:(NSUInteger)to 136 | { 137 | if (to > self.length ) 138 | { 139 | return nil; 140 | } 141 | return [self ls_substringToIndex:to]; 142 | } 143 | 144 | - (NSString *)ls_substringWithRange:(NSRange)range 145 | { 146 | if (range.location > self.length) 147 | { 148 | return nil; 149 | } 150 | 151 | if (range.length > self.length) 152 | { 153 | return nil; 154 | } 155 | 156 | if ((range.location + range.length) > self.length) 157 | { 158 | return nil; 159 | } 160 | return [self ls_substringWithRange:range]; 161 | } 162 | 163 | @end 164 | -------------------------------------------------------------------------------- /RuntimeDemo/TestClass+Category.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestClass+Category.h 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/6/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "TestClass.h" 10 | 11 | @interface TestClass (Category) 12 | 13 | @property (nonatomic, copy) NSString *categroyProperty; 14 | 15 | - (void)categoryClassMethod; 16 | 17 | - (void)categoryMethod; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /RuntimeDemo/TestClass+Category.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestClass+Category.m 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/6/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "TestClass+Category.h" 10 | #import 11 | 12 | @implementation TestClass (Category) 13 | 14 | #pragma mark - 替换原有的方法 15 | 16 | + (void)load 17 | { 18 | [TestClass swapClassMethod:@selector(classMethod:) currentMethod:@selector(myClassMethod:)]; 19 | [TestClass swapMethod:@selector(publicMethod:) currentMethod:@selector(myPublicMethod:)]; 20 | } 21 | 22 | + (void)myClassMethod:(NSString *)param 23 | { 24 | //这里调用的实际上是classMethod的实现 25 | [self myClassMethod:param]; 26 | 27 | NSLog(@"%s",__FUNCTION__); 28 | } 29 | 30 | - (void)myPublicMethod:(NSString *)param 31 | { 32 | //这里调用的实际上是publicMethod的实现 33 | [self myPublicMethod:param]; 34 | 35 | NSLog(@"%s",__FUNCTION__); 36 | } 37 | 38 | #pragma mark - 分类中属性的getter方法和setter方法 39 | 40 | - (NSString *)categroyProperty 41 | { 42 | return objc_getAssociatedObject(self, @selector(categroyProperty)); 43 | } 44 | 45 | - (void)setCategroyProperty:(NSString *)categroyProperty 46 | { 47 | objc_setAssociatedObject(self, @selector(categroyProperty), categroyProperty, OBJC_ASSOCIATION_COPY_NONATOMIC); 48 | } 49 | 50 | #pragma mark - 分类中的类方法 51 | 52 | - (void)categoryClassMethod 53 | { 54 | 55 | } 56 | 57 | #pragma mark - 分类中的实例方法 58 | 59 | - (void)categoryMethod 60 | { 61 | 62 | } 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /RuntimeDemo/TestClass.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestClass.h 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/6/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TestClass : NSObject 12 | 13 | @property (nonatomic, copy) NSString *publicProperty; 14 | 15 | + (void)classMethod:(NSString *)param; 16 | 17 | - (void)publicMethod:(NSString *)param; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /RuntimeDemo/TestClass.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestClass.m 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/6/1. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import "TestClass.h" 10 | 11 | @interface TestClass() 12 | { 13 | NSInteger _variable1; 14 | BOOL _variable2; 15 | } 16 | 17 | @property (nonatomic, strong) NSArray *privateProperty; 18 | 19 | @end 20 | 21 | @implementation TestClass 22 | 23 | + (void)classMethod:(NSString *)param; 24 | { 25 | NSLog(@"%s",__FUNCTION__); 26 | } 27 | 28 | - (void)publicMethod:(NSString *)param; 29 | { 30 | NSLog(@"%s",__FUNCTION__); 31 | } 32 | 33 | - (void)privateMethod 34 | { 35 | 36 | } 37 | 38 | /** 没有找到SEL时会执行下面的方法 */ 39 | + (BOOL)resolveInstanceMethod:(SEL)sel 40 | { 41 | [self addMethod:sel methodImp:@selector(dynamicAddMethod)]; 42 | return YES; 43 | } 44 | 45 | - (void)dynamicAddMethod 46 | { 47 | NSLog(@"找不到方法时执行这里"); 48 | } 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /RuntimeDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // RuntimeDemo 4 | // 5 | // Created by lisong on 2017/4/11. 6 | // Copyright © 2017年 lisong. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "TestClass.h" 11 | #import "TestClass+Category.h" 12 | 13 | int main(int argc, char * argv[]) 14 | { 15 | @autoreleasepool 16 | { 17 | NSLog(@"TestClass的成员变量列表:\n%@", [TestClass fetchIvarList]); 18 | 19 | NSLog(@"TestClass的属性列表:\n%@", [TestClass fetchPropertyList]); 20 | 21 | NSLog(@"TestClass的实例方法列表:\n%@", [TestClass fetchInstanceMethodList]); 22 | 23 | NSLog(@"TestClass的类方法列表:\n%@", [TestClass fetchClassMethodList]); 24 | 25 | NSLog(@"TestClass的协议列表:\n%@", [TestClass fetchProtocolList]); 26 | 27 | // 测试执行被替换方法 28 | TestClass *instance = [[TestClass alloc] init]; 29 | [instance publicMethod:@"aaa"]; 30 | [TestClass classMethod:@"aaa"]; 31 | 32 | // 测试找不到方法时会执行我们添加的方法 33 | id obj = [[TestClass alloc] init]; 34 | [obj length]; 35 | [obj objectAtIndex:2]; 36 | 37 | 38 | 39 | // 测试一些常见崩溃被处理 40 | NSString *string = nil; 41 | 42 | NSArray *array = @[string]; 43 | NSLog(@"%@",array[2]); 44 | [array objectAtIndex:2]; 45 | 46 | NSDictionary *dic = @{@"key1":@"value",@"key2":string}; 47 | NSLog(@"%@",dic); 48 | 49 | NSMutableArray *mutArray = [NSMutableArray array]; 50 | [mutArray objectAtIndex:3]; 51 | [mutArray addObject:string]; 52 | [mutArray insertObject:@"ddd" atIndex:5]; 53 | [mutArray removeObjectAtIndex:10]; 54 | 55 | NSMutableDictionary *mutDic = [NSMutableDictionary dictionary]; 56 | [mutDic setObject:string forKey:@"aaa"]; 57 | 58 | NSString *str = @"string"; 59 | 60 | [str substringToIndex:100]; 61 | [str substringWithRange:NSMakeRange(0, 100)]; 62 | } 63 | } 64 | --------------------------------------------------------------------------------