Redux-like State Container in SwiftUI 项目教程
redux-like-state-container-in-swiftuiSample project for the series of posts about building Redux-like Single Source of Truth in SwiftUI.项目地址:https://gitcode.com/gh_mirrors/re/redux-like-state-container-in-swiftui
1. 项目的目录结构及介绍
redux-like-state-container-in-swiftui/
├── README.md
├── LICENSE
├── Package.swift
├── Sources/
│ └── ReduxLikeStateContainer/
│ ├── Store.swift
│ ├── Reducer.swift
│ ├── Action.swift
│ ├── State.swift
│ └── Environment.swift
├── Tests/
│ └── ReduxLikeStateContainerTests/
│ └── ReduxLikeStateContainerTests.swift
└── Examples/
└── ExampleApp/
├── AppDelegate.swift
├── SceneDelegate.swift
├── ContentView.swift
└── Preview Content/
└── Preview Assets.xcassets
目录结构介绍
- README.md: 项目说明文档。
- LICENSE: 项目许可证文件。
- Package.swift: Swift 包管理文件。
- Sources/: 源代码目录。
- ReduxLikeStateContainer/: 核心功能模块。
- Store.swift: 状态存储类。
- Reducer.swift: 状态更新逻辑。
- Action.swift: 动作定义。
- State.swift: 状态定义。
- Environment.swift: 环境配置。
- Tests/: 测试代码目录。
- ReduxLikeStateContainerTests/: 测试用例。
- Examples/: 示例应用目录。
- ExampleApp/: 示例应用。
- AppDelegate.swift: 应用代理。
- SceneDelegate.swift: 场景代理。
- ContentView.swift: 主视图。
- Preview Content/: 预览资源。
2. 项目的启动文件介绍
AppDelegate.swift
import UIKit
import SwiftUI
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 创建一个 SwiftUI 视图
let contentView = ContentView()
// 使用 UIHostingController 作为根视图控制器
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
return true
}
}
SceneDelegate.swift
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// 创建一个 SwiftUI 视图
let contentView = ContentView()
// 使用 UIHostingController 作为根视图控制器
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}
3. 项目的配置文件介绍
Package.swift
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "ReduxLikeStateContainer",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(
name: "ReduxLikeStateContainer",
targets: ["ReduxLikeStateContainer"]),
],
dependencies: [],
targets: [
.target(
name: "ReduxLikeStateContainer",
dependencies: []),
.testTarget(
name: "ReduxLikeStateContainerTests",
dependencies: ["ReduxLikeStateContainer"]),
]
)
配置文件介绍
redux-like-state-container-in-swiftuiSample project for the series of posts about building Redux-like Single Source of Truth in SwiftUI.项目地址:https://gitcode.com/gh_mirrors/re/redux-like-state-container-in-swiftui