gpt4 book ai didi

ios - Controller 中泛型类的属性注入(inject)

转载 作者:行者123 更新时间:2023-11-30 11:47:55 26 4
gpt4 key购买 nike

我在我的ios应用程序中构建了一个状态管理,类似于javascript中的redux。它可以工作,而且使用起来很好,但我想抽象和解耦框架中的某些部分,以实现可重用性、测试和共享。

要添加一些上下文,主要思想是创建一个 class storegetStatedispatch方法。一个结构State用户定义的内容在商店初始化时传递。

/* Store signature: class MyStore<State>: MyStoreProcotol */

let store = MyStore(state: ApplicationState())`

商店初始化后,我尝试将其注入(inject)任何符合 ConnectionProtocol 的类(例如 UIViewController,但不是强制性的)。 。在下面的代码中,我将 Controller 和存储传递给函数 injectStoreInView 。我正在尝试通过此函数将存储注入(inject) Controller 中,func injectStoreInView<State>(viewController: UIViewController, store: MyStore<State>)

我尝试了几种使用泛型的方法,但没有成功。我已阅读“类型删除”并遵循教程,但我不确定它是否对这种情况有帮助以及如何应用它。我在处理参数 store 的类型时遇到麻烦的injectStoreInView,并且以后无法在 Controller 中分配它controller.connection.store .

这是代码,其中很多部分已被删除以说明问题。 (此代码不起作用。)

框架部分:

import UIKit

/* store */

protocol MyStoreProcotol {

associatedtype State

func getState() -> State

}

class MyStore<State>: MyStoreProcotol {

var state: State

init(state: State) {
self.state = state
}

func getState() -> State {
return self.state
}

}

/* connection */

protocol ConnectionProtocol {

associatedtype State

var connection: Connection<State> { get }

}

class Connection<State> {
var store: MyStore<State>? = nil
}

func injectStoreInView<State>(
viewController: UIViewController,
store: MyStore<State>
) {
if let controller = viewController /* as ConnectionProtocol */ {
controller.connection.store = store

print("yeah !")
}
}

在应用程序部分:

struct ApplicationState {
var counter = 0
}

class StartViewConnector: UIViewController, ConnectionProtocol {

let connection = Connection<ApplicationState>()

}

func appDelegatedidFinishLaunchingWithOptions() -> Bool {

let store = MyStore(state: ApplicationState())

let controller = StartViewConnector() // connector can be any class who conform to ConnectionProtocol, StartViewConnector for test but self.window?.rootViewController in iOS App

injectStoreInView(viewController: controller, store: store)

return true
}

appDelegatedidFinishLaunchingWithOptions()

最佳答案

按照@Brandon关于linkedTypesprotocolInjection的建议解决了这个问题。他在评论中提供的链接非常有帮助( How to create generic protocols in Swift? )。

此代码有效:

框架:

import UIKit

/* store */

protocol MyStoreProcotol {

associatedtype State

func getState() -> State

}

class MyStore<State>: MyStoreProcotol {

var state: State

init(state: State) {
self.state = state
}

func getState() -> State {
return self.state
}

}

/* connection */

protocol Connectable {

associatedtype Store

var connection: Connection<Store> { get }

}

protocol ConnectionProtocol {

associatedtype Store

var store: Store? { get set }

}

class Connection<Store>: ConnectionProtocol {

var store: Store? = nil

}

func injectStoreInView
<
Store: MyStoreProcotol,
Controller: Connectable
>
(
viewController: Controller,
store: Store
) where Controller.Store == Store
{
viewController.connection.store = store
}

在应用程序中的用法:

struct ApplicationState {
var counter = 0
}

class StartViewConnector: UIViewController, Connectable {

let connection = Connection<MyStore<ApplicationState>>()

}

func appDelegatedidFinishLaunchingWithOptions() -> Bool {

let store = MyStore(state: ApplicationState())

let connector = StartViewConnector()

injectStoreInView(viewController: connector, store: store)

// Now the store is properly injected, connector.connection.store = store

return true
}

appDelegatedidFinishLaunchingWithOptions()

关于ios - Controller 中泛型类的属性注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48632341/

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