TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

MVVM in SwiftUI for a Better Architecture

4 点作者 DeusExMachina大约 2 个月前

3 条评论

gustanas大约 2 个月前
Apple is creating significant friction against using MVVM—for instance, both SwiftData and AppStorage only work in a view—which makes me doubt its long-term maintainability
krzat大约 2 个月前
SwiftUI&#x27;s views are already view models, they describe how the actual view should look like.<p>The tragedy is that SwiftUI does not offer any testing instruments besides UI testing.
vanvoorden大约 2 个月前
&gt; I believe Redux-like architectural patterns […] are a terrible choice. They impose functional programming paradigms onto a language and a framework that are not functional but heavily reliant on imperative code and state.<p>Ehh… Redux shipped on JS… which was <i>less</i> functional than Swift is. Redux state ships on JS objects and has to work around mutable JS collection objects to preserve &quot;immutable&quot; value semantics. And after all that trouble it was <i>still</i> a better choice for managing global mutable state.<p>First class value types and immutable semantics in Swift make the language a solid choice for a Redux style architecture.<p>&gt; The primary issue with this approach is that it concentrates the entire application state within a single monolithic structure, resulting in tightly coupled code.<p>Ehh… not really. One big goal of Flux and Redux was to <i>decouple</i> state from views. Because transformations on state are dispatched using declarative action values on user events views no longer need to know the structure and organization of state to perform imperative logic.<p>&gt; Moreover, any change to the data in such a structure can trigger view updates across the entire view hierarchy, even in views unrelated to the updated state components.<p>This is just an implementation detail and an optimization to ship. A &quot;hello world&quot; Redux style SwiftUI application might lead to everything recomputing `body`. A production quality Redux solution for SwiftUI would probably leverage Selectors to &quot;scope&quot; slices of state similar to ReduxJS. That would mean that view components then only listen for updates from their slice… not the complete state tree.<p>The Redux community shipped this optimization almost ten years ago… there&#x27;s nothing stopping engineers in the Swift community from shipping a Redux solution with a similar optimization.<p>&gt; Another problem arises from reducers, which introduce substantial boilerplate code typically implemented using numerous Swift switches. This code blatantly violates the Open-closed principle.<p>Ehh… I&#x27;m not sure I see why structuring state transformations as pure functions is a net negative. Go ahead and critique the idea of pure function state reducers… but then we also go back and discuss the critiques for the idea of modeling state as mutable object references. It&#x27;s a tradeoff… and engineers that ship Redux in their products see why this is the correct tradeoff for them.