gpt4 book ai didi

ios - 使用 Swinject 将 Model 类实例注入(inject) Swift 中的 View 类实例

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:37 25 4
gpt4 key购买 nike

在 Swift 2.0 的单页应用程序项目中,在 XCode 7.0.1 中,使用 Swift 依赖注入(inject)框架 Swinject ,我正在执行以下操作:

复制协议(protocol).swift:

protocol DuplicateProtocol { var id: String { get } }

单例协议(protocol).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 UIKit

class 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", duplicate, singleton )
super.viewDidLoad()
}
}

AppDelegate.swift:

import Swinject
import UIKit

@UIApplicationMain
class 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 nil
AppDelegate.application
ViewController.viewDidLoad nil nil

尽可能简单地说:我需要做出哪些改变?谢谢你,JBM。

最佳答案

如果您想从 Storyboard实例化 View Controller ,则初始化器注入(inject)不可用,因为 UIKit 框架调用 init?(coder:)

相反,应该使用属性注入(inject)从 Storyboard实例化它:

class SomeViewController: UIViewController {
var duplicate: DuplicateProtocol?
var singleton: SingletonProtocol?

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

更新

Swinject 仍然不支持 UIWindow 和初始 View Controller 的隐式实例化。您需要像以下示例那样显式实例化它们。

import Swinject
import UIKit

@UIApplicationMain
class 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 具有单例(又名容器)对象范围,可以使用 .inObjectScope(.Container) as documented here 设置。 .

更新 2

Swinject 版本 0.3 支持 UIWindow 的隐式实例化及其来自“主” Storyboard的 Root View Controller 。 Here is its documentation .

关于ios - 使用 Swinject 将 Model 类实例注入(inject) Swift 中的 View 类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com