DuplicateProtocol.swift:
protocol DuplicateProtocol { var ID: String { get } }
SingletonProtocol.swift:
protocol SingletonProtocol { var ID: String { get } }
DuplicateProxy.swift
class DuplicateProxy: DuplicateProtocol { let ID: String init ( ID: String ) { self.ID = "DuplicateProxy." + ID }}
SingletonProxy.swift:
class SingletonProxy: SingletonProtocol { let ID: String init ( ID: String ) { self.ID = "SingletonProxy." + ID }}
VIEwController.swift:
import UIKitclass VIEwController: UIVIEwController{ var duplicate: DuplicateProtocol? var singleton: SingletonProtocol? required init? ( coder aDecoder: NSCoder ) { print( "VIEwController.init?",duplicate,singleton ) super.init( coder: aDecoder ) } overrIDe func vIEwDIDLoad () { print( "VIEwController.vIEwDIDLoad",singleton ) super.vIEwDIDLoad() }}
AppDelegate.swift:
import swinjectimport UIKit@UIApplicationMainclass AppDelegate: UIResponder,UIApplicationDelegate{ var window: UIWindow? func application ( application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]? ) -> Bool { let container = Container() let singleton = SingletonProxy( ID: "first" ) container.register( SingletonProtocol.self ) { _ in singleton } container.register( DuplicateProtocol.self ) { _ in DuplicateProxy( ID: "second" ) } container.registerForStoryboard( VIEwController.self ) { r,c in c.duplicate = r.resolve( DuplicateProtocol.self ) } print( "AppDelegate.application" ) return true }}
这是我不幸得到的控制台日志:
VIEwController.init? nil nilAppDelegate.applicationVIEwController.vIEwDIDLoad nil nil
尽可能简单地说:我需要做出哪些改变?谢谢你,JBM.
解决方法 如果要从故事板中实例化视图控制器,则初始化程序注入不可用,因为UIKit框架调用init?(编码器:).相反,应该使用属性注入来从故事板中实例化它:
class SomeVIEwController: UIVIEwController { var duplicate: DuplicateProtocol? var singleton: SingletonProtocol? required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }}
UPDATE
swinject不支持UIWindow和初始视图控制器的隐式实例化.您需要显式实例化它们,如下例所示.
import swinjectimport UIKit@UIApplicationMainclass AppDelegate: UIResponder,UIApplicationDelegate { var window: UIWindow? var container: Container { let container = Container() let singleton = SingletonProxy( ID: "first" ) container.register( SingletonProtocol.self ) { _ in singleton } container.register( DuplicateProtocol.self ) { _ in DuplicateProxy( ID: "second" ) } container.registerForStoryboard( VIEwController.self ) { r,c in c.duplicate = r.resolve( DuplicateProtocol.self ) } return container } func application ( application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]? ) -> Bool { let window = UIWindow(frame: UIScreen.mainScreen().bounds) window.makeKeyAndVisible() self.window = window let storyboard = swinjectStoryboard.create(name: "Main",bundle: nil,container: container) window.rootVIEwController = storyboard.instantiateInitialVIEwController() return true }}
顺便说一句,您不必自己处理单例实例. swinject具有Singleton(aka Container)对象范围,可以使用.inObjectScope(.Container)as documented here进行设置.
更新2
swinject版本0.3支持从“Main”故事板隐式实例化UIWindow及其根视图控制器. Here is its documentation.
总结以上是内存溢出为你收集整理的ios – 使用Swinject将Model类实例注入Swift中的View类实例全部内容,希望文章能够帮你解决ios – 使用Swinject将Model类实例注入Swift中的View类实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)