Skip to content

Commit

Permalink
Merge pull request #34 from yoichitgy/rename-protocols
Browse files Browse the repository at this point in the history
Renamed protocols for Swift 3 conformance
  • Loading branch information
jakubvano authored Dec 12, 2016
2 parents 51f55e5 + 9a95d71 commit 22af572
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 104 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Here is a simple example to register a dependency of a view controller without a
```swift
let container = Container()
container.storyboardInitCompleted(AnimalViewController.self) { r, c in
c.animal = r.resolve(AnimalType.self)
c.animal = r.resolve(Animal.self)
}
container.register(AnimalType.self) { _ in Cat(name: "Mimi") }
container.register(Animal.self) { _ in Cat(name: "Mimi") }
```

Next, we create an instance of `SwinjectStoryboard` with the container specified. If the container is not specified, `SwinjectStoryboard.defaultContainer` is used instead. `instantiateViewControllerWithIdentifier` method creates an instance of the view controller with its dependencies injected:
Expand All @@ -80,18 +80,18 @@ Where the classes and protocol are:

```swift
class AnimalViewController: UIViewController {
var animal: AnimalType?
var animal: Animal?

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

protocol AnimalType {
protocol Animal {
var name: String { get set }
}

class Cat: AnimalType {
class Cat: Animal {
var name: String

init(name: String) {
Expand All @@ -111,15 +111,15 @@ If a storyboard has more than one view controller with the same type, dependenci
```swift
let container = Container()
container.storyboardInitCompleted(AnimalViewController.self, name: "cat") {
r, c in c.animal = r.resolve(AnimalType.self, name: "mimi")
r, c in c.animal = r.resolve(Animal.self, name: "mimi")
}
container.storyboardInitCompleted(AnimalViewController.self, name: "dog") {
r, c in c.animal = r.resolve(AnimalType.self, name: "hachi")
r, c in c.animal = r.resolve(Animal.self, name: "hachi")
}
container.register(AnimalType.self, name: "mimi") {
container.register(Animal.self, name: "mimi") {
_ in Cat(name: "Mimi")
}
container.register(AnimalType.self, name: "hachi") {
container.register(Animal.self, name: "hachi") {
_ in Dog(name: "Hachi")
}
```
Expand All @@ -140,7 +140,7 @@ print(dogController.animal!.name) // prints "Hachi"
Where `Dog` class is:

```swift
class Dog: AnimalType {
class Dog: Animal {
var name: String

init(name: String) {
Expand All @@ -163,9 +163,9 @@ If you implicitly instantiate `UIWindow` and its root view controller from "Main
extension SwinjectStoryboard {
class func setup() {
defaultContainer.storyboardInitCompleted(AnimalViewController.self) { r, c in
c.animal = r.resolve(AnimalType.self)
c.animal = r.resolve(Animal.self)
}
defaultContainer.register(AnimalType.self) { _ in Cat(name: "Mimi") }
defaultContainer.register(Animal.self) { _ in Cat(name: "Mimi") }
}
}
```
Expand All @@ -181,16 +181,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var container: Container = {
let container = Container()
container.storyboardInitCompleted(AnimalViewController.self) { r, c in
c.animal = r.resolve(AnimalType.self)
c.animal = r.resolve(Animal.self)
}
container.register(AnimalType.self) { _ in Cat(name: "Mimi") }
container.register(Animal.self) { _ in Cat(name: "Mimi") }
return container
}()

func application(
application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.makeKeyAndVisible()
self.window = window
Expand All @@ -212,9 +212,9 @@ Storyboard Reference introduced with Xcode 7 is supported by `SwinjectStoryboard
```swift
let container = SwinjectStoryboard.defaultContainer
container.storyboardInitCompleted(AnimalViewController.self) { r, c in
c.animal = r.resolve(AnimalType.self)
c.animal = r.resolve(Animal.self)
}
container.register(AnimalType.self) { _ in Cat(name: "Mimi") }
container.register(Animal.self) { _ in Cat(name: "Mimi") }
```

If you implicitly instantiate `UIWindow` and its root view controller, the registrations setup for "Main" storyboard can be shared with the referenced storyboard since `defaultContainer` is configured in `setup` method.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwinjectStoryboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Swinject
///
/// in User Defined Runtime Attributes section on Indentity Inspector pane.
/// If no name is supplied to the registration, no runtime attribute should be specified.
public class SwinjectStoryboard: _SwinjectStoryboardBase, SwinjectStoryboardType {
public class SwinjectStoryboard: _SwinjectStoryboardBase, SwinjectStoryboardProtocol {
/// A shared container used by SwinjectStoryboard instances that are instantiated without specific containers.
///
/// Typical usecases of this property are:
Expand All @@ -38,7 +38,7 @@ public class SwinjectStoryboard: _SwinjectStoryboardBase, SwinjectStoryboardType
public override class func initialize() {
struct Static {
static var onceToken: () = {
(SwinjectStoryboard.self as SwinjectStoryboardType.Type).setup?()
(SwinjectStoryboard.self as SwinjectStoryboardProtocol.Type).setup?()
}()
}
let _ = Static.onceToken
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SwinjectStoryboardType.swift
// SwinjectStoryboardProtocol.swift
// Swinject
//
// Created by Yoichi Tagaya on 10/12/15.
Expand All @@ -12,7 +12,7 @@
// default implementation of a protocol method is always called if a class method conforming the protocol
// is defined as an extention in a different framework.
@objc
public protocol SwinjectStoryboardType {
public protocol SwinjectStoryboardProtocol {
/// Called by Swinject framework once before SwinjectStoryboard is instantiated.
///
/// - Note:
Expand Down
11 changes: 11 additions & 0 deletions Sources/UnavailableItems.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// UnavailableItems.swift
// SwinjectStoryboard
//
// Created by Yoichi Tagaya on 12/10/16.
// Copyright © 2016 Swinject Contributors. All rights reserved.
//

// MARK: For auto migration to Swinject v1.
@available(*, unavailable, renamed: "SwinjectStoryboardProtocol")
public protocol SwinjectStoryboardType { }
Loading

0 comments on commit 22af572

Please sign in to comment.