-
Notifications
You must be signed in to change notification settings - Fork 268
Swift Quick Start
let Application = Swift as TheCoursingRiver
//With all the force of a great Typhoon!
Two following steps are required to use Dependency Injection with Typhoon:
- Configure
TyphoonAssembly
subclass(es) - Use
TyphoonDefinitions
to tell the framework which classes should be considered for Dependency Injection
Create a sub-class of TyphoonAssembly, and define your class that should have a property injected (in this case we are injecting the assembly itself into the AppDelegate
)
class ApplicationAssembly : TyphoonAssembly {
dynamic func appDelegate() -> AnyObject {
return TyphoonDefinition.withClass(AppDelegate.self) {
(definition) in
definition.injectProperty("assembly", with: self)
}
}
}
Then create an entry TyphoonInitialAssemblies of type Array in your Info.plist
and add your assembly subclass name to register the assembly:
Then, add the following property to your AppDelegate:
var assembly : ApplicationAssembly?
Edit the ApplicationAssembly
you defined above to include the following definitions. (You may need to create the classes and protocols or adjust them to your current project)
dynamic func basicKnight() -> AnyObject {
return TyphoonDefinition.withClass(Knight.self) {
(definition) in
definition.useInitializer("initWithQuest:") {
(initializer) in
initializer.injectParameterWith(self.defaultQuest())
}
}
}
dynamic func defaultQuest() -> AnyObject {
return TyphoonDefinition.withClass(CampaignQuest.self)
}
Now you can access the ApplicationAssembly
in the AppDelegate:
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
}
That's it!
Before activation each method returns a TyphoonDefinition
. After activation we'll use the same interface to return built instances.
-
IMPORTANT! Every class you want to inject has to be a subclass of
NSObject
in some way (either by subclassing or adding@objc
modifier). Also if you define a protocol property in your class (e.g.var cityDao: CityDao?
), that protocol must also have the@objc
modifier). Otherwise injection will not work. See the sample project for more information.
- Here is a Swift Sample Application
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.