├── PatchSwiftDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── sunnyxx.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── sunnyxx.xcuserdatad │ └── xcschemes │ ├── PatchSwiftDemo.xcscheme │ └── xcschememanagement.plist ├── PatchSwiftDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── PatchSwiftDemo-Bridging-Header.h ├── Sark.swift ├── TestSark.swift ├── ViewController.h ├── ViewController.m └── main.m └── README.md /PatchSwiftDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 48F9492C1D2664680063D8A2 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 48F9492B1D2664680063D8A2 /* main.m */; }; 11 | 48F9492F1D2664680063D8A2 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 48F9492E1D2664680063D8A2 /* AppDelegate.m */; }; 12 | 48F949321D2664680063D8A2 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 48F949311D2664680063D8A2 /* ViewController.m */; }; 13 | 48F949351D2664680063D8A2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 48F949331D2664680063D8A2 /* Main.storyboard */; }; 14 | 48F949371D2664680063D8A2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 48F949361D2664680063D8A2 /* Assets.xcassets */; }; 15 | 48F9493A1D2664680063D8A2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 48F949381D2664680063D8A2 /* LaunchScreen.storyboard */; }; 16 | 48F9494B1D2665E30063D8A2 /* Sark.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F9494A1D2665E30063D8A2 /* Sark.swift */; }; 17 | 48F9494D1D2666500063D8A2 /* TestSark.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F9494C1D2666500063D8A2 /* TestSark.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 48F949271D2664680063D8A2 /* PatchSwiftDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PatchSwiftDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 48F9492B1D2664680063D8A2 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | 48F9492D1D2664680063D8A2 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | 48F9492E1D2664680063D8A2 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | 48F949301D2664680063D8A2 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 26 | 48F949311D2664680063D8A2 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 27 | 48F949341D2664680063D8A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | 48F949361D2664680063D8A2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | 48F949391D2664680063D8A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | 48F9493B1D2664680063D8A2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | 48F949411D2664890063D8A2 /* PatchSwiftDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "PatchSwiftDemo-Bridging-Header.h"; sourceTree = ""; }; 32 | 48F9494A1D2665E30063D8A2 /* Sark.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sark.swift; sourceTree = ""; }; 33 | 48F9494C1D2666500063D8A2 /* TestSark.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestSark.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | 48F949241D2664680063D8A2 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXFrameworksBuildPhase section */ 45 | 46 | /* Begin PBXGroup section */ 47 | 48F9491E1D2664680063D8A2 = { 48 | isa = PBXGroup; 49 | children = ( 50 | 48F949291D2664680063D8A2 /* PatchSwiftDemo */, 51 | 48F949281D2664680063D8A2 /* Products */, 52 | ); 53 | sourceTree = ""; 54 | }; 55 | 48F949281D2664680063D8A2 /* Products */ = { 56 | isa = PBXGroup; 57 | children = ( 58 | 48F949271D2664680063D8A2 /* PatchSwiftDemo.app */, 59 | ); 60 | name = Products; 61 | sourceTree = ""; 62 | }; 63 | 48F949291D2664680063D8A2 /* PatchSwiftDemo */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | 48F9494A1D2665E30063D8A2 /* Sark.swift */, 67 | 48F9494C1D2666500063D8A2 /* TestSark.swift */, 68 | 48F9492B1D2664680063D8A2 /* main.m */, 69 | 48F949441D2664EE0063D8A2 /* Useless */, 70 | 48F9492A1D2664680063D8A2 /* Supporting Files */, 71 | 48F949411D2664890063D8A2 /* PatchSwiftDemo-Bridging-Header.h */, 72 | ); 73 | path = PatchSwiftDemo; 74 | sourceTree = ""; 75 | }; 76 | 48F9492A1D2664680063D8A2 /* Supporting Files */ = { 77 | isa = PBXGroup; 78 | children = ( 79 | ); 80 | name = "Supporting Files"; 81 | sourceTree = ""; 82 | }; 83 | 48F949441D2664EE0063D8A2 /* Useless */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 48F949331D2664680063D8A2 /* Main.storyboard */, 87 | 48F949361D2664680063D8A2 /* Assets.xcassets */, 88 | 48F949381D2664680063D8A2 /* LaunchScreen.storyboard */, 89 | 48F9493B1D2664680063D8A2 /* Info.plist */, 90 | 48F9492D1D2664680063D8A2 /* AppDelegate.h */, 91 | 48F9492E1D2664680063D8A2 /* AppDelegate.m */, 92 | 48F949301D2664680063D8A2 /* ViewController.h */, 93 | 48F949311D2664680063D8A2 /* ViewController.m */, 94 | ); 95 | name = Useless; 96 | sourceTree = ""; 97 | }; 98 | /* End PBXGroup section */ 99 | 100 | /* Begin PBXNativeTarget section */ 101 | 48F949261D2664680063D8A2 /* PatchSwiftDemo */ = { 102 | isa = PBXNativeTarget; 103 | buildConfigurationList = 48F9493E1D2664680063D8A2 /* Build configuration list for PBXNativeTarget "PatchSwiftDemo" */; 104 | buildPhases = ( 105 | 48F949231D2664680063D8A2 /* Sources */, 106 | 48F949241D2664680063D8A2 /* Frameworks */, 107 | 48F949251D2664680063D8A2 /* Resources */, 108 | ); 109 | buildRules = ( 110 | ); 111 | dependencies = ( 112 | ); 113 | name = PatchSwiftDemo; 114 | productName = PatchSwiftDemo; 115 | productReference = 48F949271D2664680063D8A2 /* PatchSwiftDemo.app */; 116 | productType = "com.apple.product-type.application"; 117 | }; 118 | /* End PBXNativeTarget section */ 119 | 120 | /* Begin PBXProject section */ 121 | 48F9491F1D2664680063D8A2 /* Project object */ = { 122 | isa = PBXProject; 123 | attributes = { 124 | LastUpgradeCheck = 0730; 125 | ORGANIZATIONNAME = sunnyxx; 126 | TargetAttributes = { 127 | 48F949261D2664680063D8A2 = { 128 | CreatedOnToolsVersion = 7.3.1; 129 | }; 130 | }; 131 | }; 132 | buildConfigurationList = 48F949221D2664680063D8A2 /* Build configuration list for PBXProject "PatchSwiftDemo" */; 133 | compatibilityVersion = "Xcode 3.2"; 134 | developmentRegion = English; 135 | hasScannedForEncodings = 0; 136 | knownRegions = ( 137 | en, 138 | Base, 139 | ); 140 | mainGroup = 48F9491E1D2664680063D8A2; 141 | productRefGroup = 48F949281D2664680063D8A2 /* Products */; 142 | projectDirPath = ""; 143 | projectRoot = ""; 144 | targets = ( 145 | 48F949261D2664680063D8A2 /* PatchSwiftDemo */, 146 | ); 147 | }; 148 | /* End PBXProject section */ 149 | 150 | /* Begin PBXResourcesBuildPhase section */ 151 | 48F949251D2664680063D8A2 /* Resources */ = { 152 | isa = PBXResourcesBuildPhase; 153 | buildActionMask = 2147483647; 154 | files = ( 155 | 48F9493A1D2664680063D8A2 /* LaunchScreen.storyboard in Resources */, 156 | 48F949371D2664680063D8A2 /* Assets.xcassets in Resources */, 157 | 48F949351D2664680063D8A2 /* Main.storyboard in Resources */, 158 | ); 159 | runOnlyForDeploymentPostprocessing = 0; 160 | }; 161 | /* End PBXResourcesBuildPhase section */ 162 | 163 | /* Begin PBXSourcesBuildPhase section */ 164 | 48F949231D2664680063D8A2 /* Sources */ = { 165 | isa = PBXSourcesBuildPhase; 166 | buildActionMask = 2147483647; 167 | files = ( 168 | 48F9494D1D2666500063D8A2 /* TestSark.swift in Sources */, 169 | 48F949321D2664680063D8A2 /* ViewController.m in Sources */, 170 | 48F9492F1D2664680063D8A2 /* AppDelegate.m in Sources */, 171 | 48F9494B1D2665E30063D8A2 /* Sark.swift in Sources */, 172 | 48F9492C1D2664680063D8A2 /* main.m in Sources */, 173 | ); 174 | runOnlyForDeploymentPostprocessing = 0; 175 | }; 176 | /* End PBXSourcesBuildPhase section */ 177 | 178 | /* Begin PBXVariantGroup section */ 179 | 48F949331D2664680063D8A2 /* Main.storyboard */ = { 180 | isa = PBXVariantGroup; 181 | children = ( 182 | 48F949341D2664680063D8A2 /* Base */, 183 | ); 184 | name = Main.storyboard; 185 | sourceTree = ""; 186 | }; 187 | 48F949381D2664680063D8A2 /* LaunchScreen.storyboard */ = { 188 | isa = PBXVariantGroup; 189 | children = ( 190 | 48F949391D2664680063D8A2 /* Base */, 191 | ); 192 | name = LaunchScreen.storyboard; 193 | sourceTree = ""; 194 | }; 195 | /* End PBXVariantGroup section */ 196 | 197 | /* Begin XCBuildConfiguration section */ 198 | 48F9493C1D2664680063D8A2 /* Debug */ = { 199 | isa = XCBuildConfiguration; 200 | buildSettings = { 201 | ALWAYS_SEARCH_USER_PATHS = NO; 202 | CLANG_ANALYZER_NONNULL = YES; 203 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 204 | CLANG_CXX_LIBRARY = "libc++"; 205 | CLANG_ENABLE_MODULES = YES; 206 | CLANG_ENABLE_OBJC_ARC = YES; 207 | CLANG_WARN_BOOL_CONVERSION = YES; 208 | CLANG_WARN_CONSTANT_CONVERSION = YES; 209 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 210 | CLANG_WARN_EMPTY_BODY = YES; 211 | CLANG_WARN_ENUM_CONVERSION = YES; 212 | CLANG_WARN_INT_CONVERSION = YES; 213 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 214 | CLANG_WARN_UNREACHABLE_CODE = YES; 215 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 216 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 217 | COPY_PHASE_STRIP = NO; 218 | DEBUG_INFORMATION_FORMAT = dwarf; 219 | ENABLE_STRICT_OBJC_MSGSEND = YES; 220 | ENABLE_TESTABILITY = YES; 221 | GCC_C_LANGUAGE_STANDARD = gnu99; 222 | GCC_DYNAMIC_NO_PIC = NO; 223 | GCC_NO_COMMON_BLOCKS = YES; 224 | GCC_OPTIMIZATION_LEVEL = 0; 225 | GCC_PREPROCESSOR_DEFINITIONS = ( 226 | "DEBUG=1", 227 | "$(inherited)", 228 | ); 229 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 230 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 231 | GCC_WARN_UNDECLARED_SELECTOR = YES; 232 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 233 | GCC_WARN_UNUSED_FUNCTION = YES; 234 | GCC_WARN_UNUSED_VARIABLE = YES; 235 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 236 | MTL_ENABLE_DEBUG_INFO = YES; 237 | ONLY_ACTIVE_ARCH = YES; 238 | SDKROOT = iphoneos; 239 | TARGETED_DEVICE_FAMILY = "1,2"; 240 | }; 241 | name = Debug; 242 | }; 243 | 48F9493D1D2664680063D8A2 /* Release */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_ANALYZER_NONNULL = YES; 248 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 249 | CLANG_CXX_LIBRARY = "libc++"; 250 | CLANG_ENABLE_MODULES = YES; 251 | CLANG_ENABLE_OBJC_ARC = YES; 252 | CLANG_WARN_BOOL_CONVERSION = YES; 253 | CLANG_WARN_CONSTANT_CONVERSION = YES; 254 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 255 | CLANG_WARN_EMPTY_BODY = YES; 256 | CLANG_WARN_ENUM_CONVERSION = YES; 257 | CLANG_WARN_INT_CONVERSION = YES; 258 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 259 | CLANG_WARN_UNREACHABLE_CODE = YES; 260 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 261 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 262 | COPY_PHASE_STRIP = NO; 263 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 264 | ENABLE_NS_ASSERTIONS = NO; 265 | ENABLE_STRICT_OBJC_MSGSEND = YES; 266 | GCC_C_LANGUAGE_STANDARD = gnu99; 267 | GCC_NO_COMMON_BLOCKS = YES; 268 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 269 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 270 | GCC_WARN_UNDECLARED_SELECTOR = YES; 271 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 272 | GCC_WARN_UNUSED_FUNCTION = YES; 273 | GCC_WARN_UNUSED_VARIABLE = YES; 274 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 275 | MTL_ENABLE_DEBUG_INFO = NO; 276 | SDKROOT = iphoneos; 277 | TARGETED_DEVICE_FAMILY = "1,2"; 278 | VALIDATE_PRODUCT = YES; 279 | }; 280 | name = Release; 281 | }; 282 | 48F9493F1D2664680063D8A2 /* Debug */ = { 283 | isa = XCBuildConfiguration; 284 | buildSettings = { 285 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 286 | CLANG_ENABLE_MODULES = YES; 287 | INFOPLIST_FILE = PatchSwiftDemo/Info.plist; 288 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 289 | LIBRARY_SEARCH_PATHS = ( 290 | "$(inherited)", 291 | "$(PROJECT_DIR)/PatchSwiftDemo/libffi", 292 | ); 293 | PRODUCT_BUNDLE_IDENTIFIER = sunnyxx.PatchSwiftDemo; 294 | PRODUCT_NAME = "$(TARGET_NAME)"; 295 | SWIFT_OBJC_BRIDGING_HEADER = "PatchSwiftDemo/PatchSwiftDemo-Bridging-Header.h"; 296 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 297 | }; 298 | name = Debug; 299 | }; 300 | 48F949401D2664680063D8A2 /* Release */ = { 301 | isa = XCBuildConfiguration; 302 | buildSettings = { 303 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 304 | CLANG_ENABLE_MODULES = YES; 305 | INFOPLIST_FILE = PatchSwiftDemo/Info.plist; 306 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 307 | LIBRARY_SEARCH_PATHS = ( 308 | "$(inherited)", 309 | "$(PROJECT_DIR)/PatchSwiftDemo/libffi", 310 | ); 311 | PRODUCT_BUNDLE_IDENTIFIER = sunnyxx.PatchSwiftDemo; 312 | PRODUCT_NAME = "$(TARGET_NAME)"; 313 | SWIFT_OBJC_BRIDGING_HEADER = "PatchSwiftDemo/PatchSwiftDemo-Bridging-Header.h"; 314 | }; 315 | name = Release; 316 | }; 317 | /* End XCBuildConfiguration section */ 318 | 319 | /* Begin XCConfigurationList section */ 320 | 48F949221D2664680063D8A2 /* Build configuration list for PBXProject "PatchSwiftDemo" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | 48F9493C1D2664680063D8A2 /* Debug */, 324 | 48F9493D1D2664680063D8A2 /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | 48F9493E1D2664680063D8A2 /* Build configuration list for PBXNativeTarget "PatchSwiftDemo" */ = { 330 | isa = XCConfigurationList; 331 | buildConfigurations = ( 332 | 48F9493F1D2664680063D8A2 /* Debug */, 333 | 48F949401D2664680063D8A2 /* Release */, 334 | ); 335 | defaultConfigurationIsVisible = 0; 336 | }; 337 | /* End XCConfigurationList section */ 338 | }; 339 | rootObject = 48F9491F1D2664680063D8A2 /* Project object */; 340 | } 341 | -------------------------------------------------------------------------------- /PatchSwiftDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /PatchSwiftDemo.xcodeproj/project.xcworkspace/xcuserdata/sunnyxx.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sunnyxx/PatchSwiftDemo/db63ee3be058a38dc30ed1c549372cd1be2bd168/PatchSwiftDemo.xcodeproj/project.xcworkspace/xcuserdata/sunnyxx.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /PatchSwiftDemo.xcodeproj/xcuserdata/sunnyxx.xcuserdatad/xcschemes/PatchSwiftDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /PatchSwiftDemo.xcodeproj/xcuserdata/sunnyxx.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | PatchSwiftDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 48F949261D2664680063D8A2 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /PatchSwiftDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /PatchSwiftDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /PatchSwiftDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /PatchSwiftDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /PatchSwiftDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /PatchSwiftDemo/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 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /PatchSwiftDemo/PatchSwiftDemo-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | // in main.m 6 | extern void patchSwift(void); -------------------------------------------------------------------------------- /PatchSwiftDemo/Sark.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Sark.swift 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/6/23. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | // Patch 目标类,这是一个非继承 NSObject 的 Swift Class 12 | class Sark { 13 | func foo(bar: Int) -> Int { 14 | return bar; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /PatchSwiftDemo/TestSark.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Main.swift 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | @objc class TestSark : NSObject { 12 | class func test() { 13 | let sark = Sark() 14 | // before patch 15 | let orig = sark.foo(123); 16 | print("orig: \(orig)"); // print 123 17 | 18 | patchSwift() // in main.m 19 | 20 | // after patch 21 | let patched = sark.foo(123); 22 | print("patched: \(patched)"); // print 123 * 123 23 | } 24 | } -------------------------------------------------------------------------------- /PatchSwiftDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /PatchSwiftDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view, typically from a nib. 20 | } 21 | 22 | - (void)didReceiveMemoryWarning { 23 | [super didReceiveMemoryWarning]; 24 | // Dispose of any resources that can be recreated. 25 | } 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /PatchSwiftDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // PatchSwiftDemo 4 | // 5 | // Created by sunnyxx on 16/7/1. 6 | // Copyright © 2016年 sunnyxx. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | #import 12 | #import 13 | #import "PatchSwiftDemo-Swift.h" 14 | 15 | // patch Sark.foo(bar) 16 | int patchedFunction(int bar) { 17 | return bar * bar; 18 | } 19 | 20 | // called in TestSark.swift 21 | void patchSwift(void) { 22 | 23 | // 根据符号获取要 patch swift 方法地址 24 | void *handle = dlopen(NULL/* current image */, RTLD_GLOBAL); 25 | // 这个符号 nm 取到的,dlsym 会在符号前加下划线 26 | // 根据 swift name mangling 可以动态生成这个串 27 | int64_t *swiftFunc = dlsym(handle, "_TFC14PatchSwiftDemo4Sark3foofSiSi"); 28 | int64_t *newFunc = (int64_t *)&patchedFunction; 29 | 30 | // 原函数和 patch 函数的 offset 31 | int64_t offset = (int64_t)newFunc - ((int64_t)swiftFunc + 5 * sizeof(char)); 32 | 33 | // 将代码区这个 page 改成可写 34 | size_t pageSize = sysconf(_SC_PAGESIZE); 35 | uintptr_t start = (uintptr_t)swiftFunc; 36 | uintptr_t end = start + 1; 37 | uintptr_t pageStart = start & -pageSize; 38 | // 注意,这个方法在非越狱 iOS 真机被禁用 39 | mprotect((void *)pageStart, end - pageStart, PROT_READ | PROT_WRITE | PROT_EXEC); 40 | 41 | // swift 函数第一行指令改成 jmp offset 42 | int64_t instruction = 0xe9 | offset << 8; 43 | *swiftFunc = instruction; 44 | } 45 | 46 | int main(int argc, char * argv[]) { 47 | @autoreleasepool { 48 | [TestSark test]; // call swift 49 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 使用 `mprotect` Patch 纯 Swift 方法的 Demo,真机不可用 2 | 3 | ``` objc 4 | void patchSwift(void) { 5 | 6 | // 根据符号获取要 patch swift 方法地址 7 | void *handle = dlopen(NULL/* current image */, RTLD_GLOBAL); 8 | // 这个符号 nm 取到的,dlsym 会在符号前加下划线 9 | // 根据 swift name mangling 可以动态生成这个串 10 | int64_t *swiftFunc = dlsym(handle, "_TFC14PatchSwiftDemo4Sark3foofSiSi"); 11 | int64_t *newFunc = (int64_t *)&patchedFunction; 12 | 13 | // 原函数和 patch 函数的 offset 14 | int64_t offset = (int64_t)newFunc - ((int64_t)swiftFunc + 5 * sizeof(char)); 15 | 16 | // 将代码区这个 page 改成可写 17 | size_t pageSize = sysconf(_SC_PAGESIZE); 18 | uintptr_t start = (uintptr_t)swiftFunc; 19 | uintptr_t end = start + 1; 20 | uintptr_t pageStart = start & -pageSize; 21 | // 注意,这个方法在非越狱 iOS 真机被禁用 22 | mprotect((void *)pageStart, end - pageStart, PROT_READ | PROT_WRITE | PROT_EXEC); 23 | 24 | // swift 函数第一行指令改成 jmp offset 25 | int64_t instruction = 0xe9 | offset << 8; 26 | *swiftFunc = instruction; 27 | } 28 | ``` --------------------------------------------------------------------------------