Skip to content

Swift Quick Start

jasperblues edited this page Sep 9, 2014 · 33 revisions
let Application = Swift as TheCoursingRiver
//With all the force of a great Typhoon!

Setting up a Dependency Injection for your Swift application is easy!

  • First, let's bootstrap Typhoon.
  • Create a sub-class of TyphoonAssembly, and define your AppDelegate, as follows:
public class ApplicationAssembly : TyphoonAssembly {
    
    public dynamic func appDelegate() -> AnyObject {
        return TyphoonDefinition.withClass(AppDelegate.self) {
            (definition) in
            
            definition.injectProperty("assembly", with: self)
        }
    }    
}

###Create an entry in your application's plist to register the assembly:

###And now, add the following property to your AppDelegate:

var assembly : ApplicationAssembly?

###Now let's perform an Initializer Injection

    public dynamic func basicKnight() -> AnyObject {
        
        return TyphoonDefinition.withClass(Knight.self) {
            (definition) in
            
            definition.useInitializer("initWithQuest:") {
                (initializer) in
                
                initializer.injectParameterWith(self.defaultQuest())
                
            }
        }
    }
    
    public dynamic func defaultQuest() -> AnyObject {
        
        return TyphoonDefinition.withClass(CampaignQuest.self)
    }

###Resolve the component from the container as follows:

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //Code-completion + no 'magic strings'  
        var knight : Knight = assembly.basicKnight()
        
        // . . continue setting up App        

        return true
    }

And we're done!


  • The assembly interface can be injected on any class, not just the AppDelegate
  • Typhoon can also be manually bootstrapped, by instantiating a TyphoonComponentFactory. Note that in Objective-C you may cast the TyphoonComponentFactory to your Assembly interfaces for code completion and tight IDE integration. Not that in Swift, this is only possible if you inject the assembly onto a component, as shown above.

#User Guide

(Swift user guide coming soon)


Quick Start!

Get started in two minutes.

Main Track

Get familiar with Typhoon.

Advanced Topics

Become a Typhoon expert.

Under the Hood

For contributors or curious folks.

Clone this wiki locally