-
Notifications
You must be signed in to change notification settings - Fork 260
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
- Edit the ApplicationAssembly you defined above to include the following definitions:
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 Typhoon 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!
#Key Concept
- At build-time our Assembly interface returns TyphoonDefinitions
- At run-time we use this interface to resolve built components.
#Other Notable Points
- The assembly interface can be injected on any class, not just the AppDelegate
- Typhoon can also be manually bootstrapped, by instantiating a TyphoonComponentFactory. This is when introducing Typhoon into a legacy application. (Hopefully your Swift applications aren't this yet ;) ). Note that in Objective-C you may have an assembly pose in front of your TyphoonComponentFactory by casting it to your Assemby interface. This provides completion, and allows the use of IDE refactoring tools. 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)
- [Types of Injections](Types of Injections)
- [What can be Injected](What can be Injected)
- Modules
- Scopes
- Obtaining Built Components
- [Integration Testing](Integration Testing)
Something still not clear? How about posting a question on StackOverflow.
Get started in two minutes.
Get familiar with Typhoon.
- Types of Injections
- What can be Injected
- Auto-injection (Objective-C)
- Scopes
- Storyboards
- TyphoonLoadedView
- Activating Assemblies
Become a Typhoon expert.
For contributors or curious folks.
