{"id":3211,"url":"https://github.com/Swinject/Swinject","last_synced_at":"2025-08-06T13:32:18.439Z","repository":{"id":35881882,"uuid":"40167453","full_name":"Swinject/Swinject","owner":"Swinject","description":"Dependency injection framework for Swift with iOS/macOS/Linux","archived":false,"fork":false,"pushed_at":"2024-07-24T23:14:24.000Z","size":1750,"stargazers_count":6591,"open_issues_count":71,"forks_count":541,"subscribers_count":103,"default_branch":"master","last_synced_at":"2025-08-05T07:03:02.079Z","etag":null,"topics":["dependency-injection","di-container","inversion-of-control","ioc-container","ios","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/Swinject.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2015-08-04T06:31:13.000Z","updated_at":"2025-08-04T08:38:59.000Z","dependencies_parsed_at":"2024-04-12T19:25:29.124Z","dependency_job_id":"391f007f-7214-423e-ba15-a97d2a0e2325","html_url":"https://github.com/Swinject/Swinject","commit_stats":{"total_commits":783,"total_committers":55,"mean_commits":"14.236363636363636","dds":"0.44955300127713926","last_synced_commit":"dfb78563c581eea560ea90c19dbe8d1d93d11260"},"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"purl":"pkg:github/Swinject/Swinject","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swinject%2FSwinject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swinject%2FSwinject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swinject%2FSwinject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swinject%2FSwinject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Swinject","download_url":"https://codeload.github.com/Swinject/Swinject/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swinject%2FSwinject/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268891799,"owners_count":24324624,"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","status":"online","status_checked_at":"2025-08-05T02:00:12.334Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["dependency-injection","di-container","inversion-of-control","ioc-container","ios","swift"],"created_at":"2024-01-05T20:16:34.600Z","updated_at":"2025-08-06T13:32:18.132Z","avatar_url":"https://github.com/Swinject.png","language":"Swift","funding_links":[],"categories":["Dependency Injection","Libs","Injection","Swift","WebSocket","awesome-ios ##","Architecture and State","Dependency Injection [🔝](#readme)"],"sub_categories":["Getting Started","Dependency Injection","Other free courses","API","Web View"],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://avatars0.githubusercontent.com/u/13637225?v=3\u0026s=600\" width=\"33%\"\u003e\n\u003c/p\u003e\n\nSwinject\n========\n\n![Github Actions](https://github.com/1ucas/Swinject/actions/workflows/Project%20Testing.yml/badge.svg?branch=master)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![CocoaPods Version](https://img.shields.io/cocoapods/v/Swinject.svg?style=flat)](http://cocoapods.org/pods/Swinject)\n[![License](https://img.shields.io/cocoapods/l/Swinject.svg?style=flat)](http://cocoapods.org/pods/Swinject)\n[![Platforms](https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux-lightgrey.svg)](http://cocoapods.org/pods/Swinject)\n[![Swift Version](https://img.shields.io/badge/Swift-4.2--5.4-F16D39.svg?style=flat)](https://developer.apple.com/swift)\n[![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)\n\nSwinject is a lightweight [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) framework for Swift.\n\nDependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) for resolving dependencies. In the pattern, Swinject helps your app split into loosely-coupled components, which can be developed, tested and maintained more easily. Swinject is powered by the Swift generic type system and first class functions to define dependencies of your app simply and fluently.\n\nSwinject is maintained by the [Faire Wholesale Inc.](https://github.com/Faire?view_as=public) mobile platform team.\n\n## Features\n\n- [x] [Pure Swift Type Support](./Documentation/README.md#user-content-pure-swift-type-support)\n- [x] [Injection with Arguments](./Documentation/DIContainer.md#registration-with-arguments-to-di-container)\n- [x] [Initializer/Property/Method Injections](./Documentation/InjectionPatterns.md)\n- [x] [Initialization Callback](./Documentation/InjectionPatterns.md#user-content-initialization-callback)\n- [x] [Circular Dependency Injection](./Documentation/CircularDependencies.md)\n- [x] [Object Scopes as None (Transient), Graph, Container (Singleton) and Hierarchy](./Documentation/ObjectScopes.md)\n- [x] Support of both Reference and [Value Types](./Documentation/Misc.md#value-types)\n- [x] [Self-registration (Self-binding)](./Documentation/Misc.md#self-registration-self-binding)\n- [x] [Container Hierarchy](./Documentation/ContainerHierarchy.md)\n- [x] [Thread Safety](./Documentation/ThreadSafety.md)\n- [x] [Modular Components](./Documentation/Assembler.md)\n\n## Extensions\n\n- **[SwinjectPropertyLoader](https://github.com/Swinject/SwinjectPropertyLoader)**: Loading property values from resources.\n- **[SwinjectStoryboard](https://github.com/Swinject/SwinjectStoryboard)**: Automatic dependency injection via Storyboard.\n- **[Swinject-CodeGen](https://github.com/Swinject/Swinject-CodeGen)**: Type-safe code generation of `Container` from a CSV/YAML file defining dependencies.\n- **[SwinjectAutoregistration](https://github.com/Swinject/SwinjectAutoregistration)**: Automatic registration of services by leveraging the Swift Generics.\n\n## Requirements\n\n- iOS 11.0+ / Mac OS X 10.13+ / watchOS 4.0+ / tvOS 11.0+\n- Xcode 14.3+\n- Swift 4.2+\n- Carthage 0.18+ (if you use)\n- CocoaPods 1.1.1+ (if you use)\n\n## Installation\n\nSwinject is available through [Carthage](https://github.com/Carthage/Carthage), [CocoaPods](https://cocoapods.org), or [Swift Package Manager](https://swift.org/package-manager/).\n\n### Carthage\n\nTo install Swinject with Carthage, add the following line to your `Cartfile`.\n\n```\ngithub \"Swinject/Swinject\"\n\n# Uncomment if you use SwinjectStoryboard\n# github \"Swinject/SwinjectStoryboard\"\n```\n\nThen run `carthage update --no-use-binaries` command or just `carthage update`. For details of the installation and usage of Carthage, visit [its project page](https://github.com/Carthage/Carthage).\n\n\n### CocoaPods\n\nTo install Swinject with CocoaPods, add the following lines to your `Podfile`.\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '11.0' # or platform :osx, '10.13' if your target is OS X.\nuse_frameworks!\n\npod 'Swinject'\n\n# Uncomment if you use SwinjectStoryboard\n# pod 'SwinjectStoryboard'\n```\n\nThen run `pod install` command. For details of the installation and usage of CocoaPods, visit [its official website](https://cocoapods.org).\n\n### Swift Package Manager\n\nin `Package.swift` add the following:\n\n```swift\ndependencies: [\n    // Dependencies declare other packages that this package depends on.\n    // .package(url: /* package url */, from: \"1.0.0\"),\n    .package(url: \"https://github.com/Swinject/Swinject.git\", from: \"2.8.0\")\n],\ntargets: [\n    .target(\n        name: \"MyProject\",\n        dependencies: [..., \"Swinject\"]\n    )\n    ...\n]\n```\n\n## Documentation\n\n- [Technical documents](./Documentation) including patterns of dependency injection and examples.\n- [API reference](http://cocoadocs.org/docsets/Swinject/)\n\n## Basic Usage\n\nFirst, register a service and component pair to a `Container`, where the component is created by the registered closure as a factory. In this example, `Cat` and `PetOwner` are component classes implementing `Animal` and `Person` service protocols, respectively.\n\n```swift\nlet container = Container()\ncontainer.register(Animal.self) { _ in Cat(name: \"Mimi\") }\ncontainer.register(Person.self) { r in\n    PetOwner(pet: r.resolve(Animal.self)!)\n}\n```\n\nThen get an instance of a service from the container. The person is resolved to a pet owner, and playing with the cat named Mimi!\n\n```swift\nlet person = container.resolve(Person.self)!\nperson.play() // prints \"I'm playing with Mimi.\"\n```\n\nWhere definitions of the protocols and classes are\n\n```swift\nprotocol Animal {\n    var name: String? { get }\n}\n\nclass Cat: Animal {\n    let name: String?\n\n    init(name: String?) {\n        self.name = name\n    }\n}\n```\n\nand\n\n```swift\nprotocol Person {\n    func play()\n}\n\nclass PetOwner: Person {\n    let pet: Animal\n\n    init(pet: Animal) {\n        self.pet = pet\n    }\n\n    func play() {\n        let name = pet.name ?? \"someone\"\n        print(\"I'm playing with \\(name).\")\n    }\n}\n```\n\nNotice that the `pet` of `PetOwner` is automatically set as the instance of `Cat` when `Person` is resolved to the instance of `PetOwner`. If a container already set up is given, you do not have to care what are the actual types of the services and how they are created with their dependency.\n\n## Where to Register Services\n\nServices must be registered to a container before they are used. The typical registration approach will differ depending upon whether you are using `SwinjectStoryboard` or not.\n\nThe following view controller class is used in addition to the protocols and classes above in the examples below.\n\n```swift\nclass PersonViewController: UIViewController {\n    var person: Person?\n}\n```\n\n### With SwinjectStoryboard\n\nImport SwinjectStoryboard at the top of your swift source file.\n\n```swift\nimport SwinjectStoryboard\n```\n\nServices should be registered in an extension of `SwinjectStoryboard` if you use `SwinjectStoryboard`. Refer to [the project page of SwinjectStoryboard](https://github.com/Swinject/SwinjectStoryboard) for further details.\n\n```swift\nextension SwinjectStoryboard {\n    @objc class func setup() {\n        defaultContainer.register(Animal.self) { _ in Cat(name: \"Mimi\") }\n        defaultContainer.register(Person.self) { r in\n            PetOwner(pet: r.resolve(Animal.self)!)\n        }\n        defaultContainer.register(PersonViewController.self) { r in\n            let controller = PersonViewController()\n            controller.person = r.resolve(Person.self)\n            return controller\n        }\n    }\n}\n```\n\n### Without SwinjectStoryboard\n\nIf you do not use `SwinjectStoryboard` to instantiate view controllers, services should be registered to a container in your application's `AppDelegate`. Registering before exiting `application:didFinishLaunchingWithOptions:` will ensure that the services are setup appropriately before they are used.\n\n```swift\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n    var window: UIWindow?\n    let container: Container = {\n        let container = Container()\n        container.register(Animal.self) { _ in Cat(name: \"Mimi\") }\n        container.register(Person.self) { r in\n            PetOwner(pet: r.resolve(Animal.self)!)\n        }\n        container.register(PersonViewController.self) { r in\n            let controller = PersonViewController()\n            controller.person = r.resolve(Person.self)\n            return controller\n        }\n        return container\n    }()\n\n    func application(\n        _ application: UIApplication,\n        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -\u003e Bool {\n\n        // Instantiate a window.\n        let window = UIWindow(frame: UIScreen.main.bounds)\n        window.makeKeyAndVisible()\n        self.window = window\n\n        // Instantiate the root view controller with dependencies injected by the container.\n        window.rootViewController = container.resolve(PersonViewController.self)\n\n        return true\n    }\n}\n```\n\nNotice that the example uses a convenience initializer taking a closure to register services to the new instance of `Container`.\n\n## Play in Playground!\n\nThe project contains `Sample-iOS.playground` to demonstrate the features of Swinject. Download or clone the project, run the playground, modify it, and play with it to learn Swinject.\n\nTo run the playground in the project, first build the project, then select `Editor \u003e Execute Playground` menu in Xcode.\n\n## Example Apps\n\nSome example apps using Swinject can be found on [GitHub](https://github.com/search?o=desc\u0026q=swinject\u0026s=updated\u0026type=Repositories).\n\n## Blog Posts\n\nThe following blog posts introduce the concept of dependency injection and Swinject.\n\n- [Dependency Injection Tutorial for iOS: Getting Started](https://www.raywenderlich.com/14223279-dependency-injection-tutorial-for-ios-getting-started) *by [Irina Galata](https://www.raywenderlich.com/u/galata)*\n- [Swinject Tutorial for iOS: Getting Started](https://www.raywenderlich.com/17-swinject-tutorial-for-ios-getting-started) *by [Gemma Barlow](https://www.raywenderlich.com/u/gemmakbarlow)*\n- [iOS Dependency Injection Using Swinject](https://medium.com/flawless-app-stories/ios-dependency-injection-using-swinject-9c4ceff99e41) *by [Ali Akhtar](https://ali-akhtar.medium.com)*\n- [DI in iOS: Complete guide](https://medium.com/its-tinkoff/di-in-ios-complete-guide-cd76a079d2d) *by [Vitaly Batrakov](https://medium.com/@batrakov.vitaly)*\n\nThanks the authors!\n\n## Contribution Guide\n\nA guide to [submit issues](https://github.com/Swinject/Swinject/issues), to ask general questions, or to [open pull requests](https://github.com/Swinject/Swinject/pulls) is [here](CONTRIBUTING.md).\n\n## Question?\n\n- [Stack Overflow](https://stackoverflow.com/questions/tagged/swinject) we are trying to monitor questions tagged `swinject`\n\n## Credits\n\nThe DI container features of Swinject are inspired by:\n\n- [Ninject](http://ninject.org) - [Enkari, Ltd](https://github.com/enkari) and [the Ninject project contributors](https://github.com/ninject/Ninject/graphs/contributors).\n- [Autofac](http://autofac.org) - [Autofac Project](https://github.com/autofac/Autofac).\n\nand highly inspired by:\n\n- [Funq](https://github.com/kzu/funq) - [Daniel Cazzulino](https://github.com/kzu).\n\n## License\n\nMIT license. See the [LICENSE file](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwinject%2FSwinject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSwinject%2FSwinject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwinject%2FSwinject/lists"}