SwiftUI-如何将EnvironmentObject传递到视图模型中? [英] SwiftUI - How to pass EnvironmentObject into View Model?

查看:186
本文介绍了SwiftUI-如何将EnvironmentObject传递到视图模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找创建一个可以由视图模型(而不仅仅是视图)访问的EnvironmentObject.

I'm looking to create an EnvironmentObject that can be accessed by the View Model (not just the view).

Environment对象跟踪应用程序会话数据,例如登录,访问令牌等,这些数据将传递到视图模型(或需要的服务类)中,以允许调用API来传递来自此EnvironmentObjects的数据.

The Environment object tracks the application session data, e.g. loggedIn, access token etc, this data will be passed into the view models (or service classes where needed) to allow calling of an API to pass data from this EnvironmentObjects.

我试图将会话对象从视图传递给视图模型类的初始化程序,但会出错.

I have tried to pass in the session object to the initialiser of the view model class from the view but get an error.

如何使用SwiftUI将EnvironmentObject访问/传递到视图模型?

how can I access/pass the EnvironmentObject into the view model using SwiftUI?

推荐答案

不应该.常见的误解是,SwiftUI与MVVM配合使用效果最佳.

You shouldn't. It's a common misconception that SwiftUI works best with MVVM.

MVVM在SwfitUI中没有位置.您是在问是否可以将矩形推到

MVVM has no place in SwfitUI. You are asking that if you can shove a rectangle to

适合三角形.它不合适.

fit a triangle shape. It wouldn't fit.

让我们从一些事实开始并逐步进行工作:

Let's start with some facts and work step by step:

  1. ViewModel是MVVM中的模型.

  1. ViewModel is a model in MVVM.

MVVM不考虑值类型(例如,java中没有此类内容).

MVVM does not take value type (e.g.; no such thing in java)into consideration.

值类型模型(无状态模型)被认为比参考模型更安全

A value type model (model without state) is considered safer than reference

型模型(具有状态的模型).

type model (model with state) in the sense of immutability.

现在,MVVM要求您以这样一种方式建立模型,即每当模型更改时,

Now, MVVM requires you to set up a model in such way that whenever it changes, it

以某种预定的方式更新视图.这称为绑定.

updates view in some pre-determined way. This is known as binding.

没有约束,您将无法很好地分离关注点,例如;重构

Without binding, you won't have nice separation of concerns, e.g.; refactoring out

模型和关联状态,并使它们与视图分离.

model and associated states and keeping them separate from view.

这是大多数iOS MVVM开发人员失败的两件事:

These are the two things most iOS MVVM developers fail:

  1. iOS在传统的Java意义上没有绑定"机制.

  1. iOS has no "binding" mechanism in traditional java sense.

有些人只是忽略绑定,而认为调用对象ViewModel

Some would just ignore binding, and think calling an object ViewModel

自动解决所有问题;有些人会引入基于KVO的Rx,并且

automagically solves everything; some would introduce KVO-based Rx, and

当MVVM使事情变得更简单时,会使一切变得复杂.

complicate everything when MVVM is supposed to make things simpler.

具有状态的模型太危险了

model with state is just too dangerous

因为MVVM过分强调ViewModel,而过分强调状态管理

because MVVM put too much emphasis on ViewModel, too little on state management

以及控制管理的一般学科;大多数开发人员最终都成为

and general disciplines in managing Control; most of the developers end up

认为具有用于更新视图的状态的模型是 reusable

thinking a model with state that is used to update view is reusable and

可测试.

这就是为什么Swift首先引入值类型的原因;没有

this is why Swift introduces value type in the first place; a model without

状态.

现在问您一个问题:您问ViewModel是否可以访问EnvironmentObject(EO)?

Now to your question: you ask if your ViewModel can have access to EnvironmentObject (EO)?

不应该.因为在SwiftUI中,符合View的模型会自动具有

You shouldn't. Because in SwiftUI a model that conforms to View automatically have

对EO的引用.例如;

reference to EO. E.g.;

struct Model: View {
    @EnvironmentObject state: State
    // automatic binding in body
    var body: some View {...}
}

我希望人们能够欣赏紧凑型SDK的设计方式.

I hope people can appreciate how compact SDK is designed.

在SwiftUI中,MVVM是自动.不需要单独的ViewModel对象

In SwiftUI, MVVM is automatic. There's no need for a separate ViewModel object

手动绑定到需要传递EO引用的视图.

that manually binds to view which requires an EO reference passed to it.

上面的代码 MVVM.例如.;具有绑定到视图的模型.

The above code is MVVM. E.g.; a model with binding to view.

但是因为模型是值类型,所以与其将模型和状态重构为

But because model is value type, so instead of refactoring out model and state as

视图模型,您可以重构控制权(例如,在协议扩展中).

view model, you refactor out control (in protocol extension, for example).

这是官方的SDK,其设计模式适应了语言功能,而不仅仅是

This is official SDK adapting design pattern to language feature, rather than just

强制执行.实质重于形式.

enforcing it. Substance over form.

看看您的解决方案,您必须使用基本上是全局的单例.你

Look at your solution, you have to use singleton which is basically global. You

应该知道在没有

不变性,因为您必须使用引用类型模型,所以您没有它!

immutability, which you don't have because you have to use reference type model!

TL; DR

您不会在SwiftUI中以Java方式执行MVVM.不需要Swift-y的方法

You don't do MVVM in java way in SwiftUI. And the Swift-y way to do it is no need

要做到这一点,它已经内置.

to do it, it's already built-in.

希望更多的开发人员能够看到这一点,因为这似乎是一个受欢迎的问题.

Hope more developer see this since this seemed like a popular question.

这篇关于SwiftUI-如何将EnvironmentObject传递到视图模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆