Redux-like State Container in SwiftUI 项目教程

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"]),
    ]
)

配置文件介绍

  • Package.swift: 定义了 Swift

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

章炎滔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值