{"id":22848720,"url":"https://github.com/kealdishx/swiftloadhook","last_synced_at":"2025-07-05T03:36:04.100Z","repository":{"id":143700787,"uuid":"141008547","full_name":"kealdishx/SwiftLoadHook","owner":"kealdishx","description":"Use a hack way to achieve similar functions as Load() or initialize() in OC","archived":false,"fork":false,"pushed_at":"2018-07-28T02:14:43.000Z","size":27,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T06:12:41.841Z","etag":null,"topics":["hook","initializer","ios","load","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kealdishx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-15T07:49:52.000Z","updated_at":"2024-11-01T16:53:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"92cba3b7-3ded-4d6f-bcf0-0ff35fa3cdfa","html_url":"https://github.com/kealdishx/SwiftLoadHook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kealdishx/SwiftLoadHook","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kealdishx%2FSwiftLoadHook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kealdishx%2FSwiftLoadHook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kealdishx%2FSwiftLoadHook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kealdishx%2FSwiftLoadHook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kealdishx","download_url":"https://codeload.github.com/kealdishx/SwiftLoadHook/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kealdishx%2FSwiftLoadHook/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263677006,"owners_count":23494615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hook","initializer","ios","load","swift"],"created_at":"2024-12-13T04:13:53.076Z","updated_at":"2025-07-05T03:36:04.080Z","avatar_url":"https://github.com/kealdishx.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftLoadHook [中文介绍](https://github.com/iiiCeBlink/SwiftLoadHook/blob/master/README_CN.md)\n\n## Purpose\n\nThis lib uses a hack way to achieve similar functions as `Load()` or `initialize()`. \n\n## Reason\n\nUpon migrating a project to Swift 3.1, Xcode raises a warning:\n\n\u003e Method ‘initialize()’ defines Objective-C class method ‘initialize’, which is not guaranteed to be invoked by Swift and will be disallowed in future versions.\n\n## Requirements\n\n- iOS 8.0+\n- swift 3.0+\n\n## Usage\n\nFirst, drop files under `Sources` folder to your project.\n\nThen, your target class should conforms to `SelfAware` protocol, and implements the functions in `SelfAware` protocol.\n\nFinally, write the code you want in `awake` function just like that in `Load()` or `Initialize()`.\n\n## Example\n\nThis example is used to help you understand how to use, you can find the code in the files under `Example` folder. In this example, I want to swizzle the IMP of function `viewWillAppear()` in `UIViewController`.\n\nFirst, `UIViewController` should conform to `SelfAware` protocol and implement functions of the protocol.\n\n```swift\nextension UIViewController: SelfAware {\n\n  static func awake() {\n    UIViewController.classInit()\n  }\n\n  static func classInit() {\n    swizzleMethod\n  }\n}\n```\n\nThen, I should implement `swizzleMethod` function. while swizzling methods, we should use `dispatch_once` in `Load()` or `Initialize()` function. However, since swift 3.x, we cannot find `dispatch_once` in API. How can we handle this? We can use `static let instance` to handle with it.\n\n```swift\n@objc func swizzled_viewWillAppear(_ animated: Bool) {\n    swizzled_viewWillAppear(animated)\n    print(\"swizzled_viewWillAppear\")\n  }\n\n  private static let swizzleMethod: Void = {\n    let originalSelector = #selector(viewWillAppear(_:))\n    let swizzledSelector = #selector(swizzled_viewWillAppear(_:))\n    swizzlingForClass(UIViewController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)\n  }()\n\n  private static func swizzlingForClass(_ forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {\n\n    let originalMethod = class_getInstanceMethod(forClass, originalSelector)\n    let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)\n\n    guard (originalMethod != nil \u0026\u0026 swizzledMethod != nil) else {\n      return\n    }\n\n    if class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!)) {\n      class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))\n    } else {\n      method_exchangeImplementations(originalMethod!, swizzledMethod!)\n    }\n  }\n```\n\n## Thanks\n\n@JORDAN SMITH\n\n## Reference\n\n[Handling the Deprecation of initialize()](http://jordansmith.io/handling-the-deprecation-of-initialize/)\n\n## License\n\nSwiftLoadHook is released under the MIT license. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkealdishx%2Fswiftloadhook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkealdishx%2Fswiftloadhook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkealdishx%2Fswiftloadhook/lists"}