按照这个它工作正常。
但是,当我尝试用同样的方法解决嵌套依赖项时(依赖注入的类又具有依赖项——在我们的例子中是 NetworkService),它崩溃了。我在这里做错了什么?任何帮助将不胜感激。
实时场景
class AppContainer {
static let shared = AppContainer()
var index: [Any] = [NetworkingLibrary(), NetworkService()]
func resolve<T>(_ type: T.Type) -> T {
return index.first(where: { $0 as? T != nil }) as! T
}
}
@propertyWrapper
struct Inject<Value> {
var value: Value
var wrappedValue: Value {
get {
return value
}
set {
value = newValue
}
}
init(_ container: AppContainer = AppContainer.shared) {
value = container.resolve(Value.self)
}
}
class UserService {
@Inject() var networkLayer: NetworkLayer
init() { }
func fetchUsers() {
networkLayer.fetchData()
}
}
class PostService {
@Inject() var networkLayer: NetworkLayer
init() { }
func fetchPosts() {
networkLayer.fetchData()
}
}
protocol NetworkLayer {
func fetchData()
}
class NetworkService: NetworkLayer {
@Inject() var networkLibrary: NetworkingLibraryProtocol //Expecting networkLibrary to be resolved to NetworkingLibrary()
func fetchData() {
networkLibrary.fetch()
print("Fetching Data")
}
}
let postService = PostService() // crashes here
postService.fetchPosts()
let userService = UserService() // crashes here
userService.fetchUsers()
你可以在操场上运行的代码
class AppContainer {
static let shared = AppContainer()
var index: [Any] = ["StackOverflow", NetworkService()]
func resolve<T>(_ type: T.Type) -> T {
return index.first(where: { $0 as? T != nil }) as! T
}
}
@propertyWrapper
struct Inject<Value> {
var value: Value
var wrappedValue: Value {
get {
return value
}
set {
value = newValue
}
}
init(_ container: AppContainer = AppContainer.shared) {
value = container.resolve(Value.self)
}
}
class UserService {
@Inject() var networkLayer: NetworkLayer
init() { }
func fetchUsers() {
networkLayer.fetchData()
}
}
class PostService {
@Inject() var networkLayer: NetworkLayer
init() { }
func fetchPosts() {
networkLayer.fetchData()
}
}
protocol NetworkLayer {
func fetchData()
}
class NetworkService: NetworkLayer {
@Inject() var str: String // Expecting str to be resolved to "StackOverflow"
func fetchData() {
print(str)
print("Fetching Data")
}
}
let postService = PostService()
postService.fetchPosts()
let userService = UserService()
userService.fetchUsers()
崩溃日志:-
错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)。进程一直停留在被中断的地方,使用“thread return -x”返回表达式求值前的状态。