Building a Family Score App with SwiftUI 使用 SwiftUI 构建家庭评分应用程序 Building a Family Score App with SwiftUI 使用 SwiftUI 构建家庭评分应用程序 In this tutorial, we'll walk through the process of creating a Family Score App using SwiftUI. This app allows users to keep track of scores for different family members, with features like adding new members, adjusting scores, and persisting data. 在本教程中,我们将介绍使用 SwiftUI 创建家庭分数应用程序的过程。该应用程序允许用户跟踪不同家庭成员的分数,具有添加新成员、调整分数和持久化数据等功能。

Step 1: Setting up the Project 步骤 1:设置项目 First, create a new SwiftUI project in Xcode. Name it "FamilyScore". 首先,在 Xcode 中创建一个新的 SwiftUI 项目。将其命名为 "FamilyScore"。

Step 2: Creating the Data Model 步骤 2:创建数据模型 Let's start by defining our data model. Create a new Swift file called Score.swift: 让我们从定义数据模型开始。新建一个名为 Score.swift 的 Swift 文件:

// // Score.swift // FamilyScore // // Created by william on 2024/10/23. //

import Foundation

struct Score: Codable, Identifiable, Hashable { var id = UUID() var familyMemberName = "Family Name" var score = 100 var colour = ColourHelper.blue

static let example = Score()

} This struct represents a family member's score, with properties for id, name, score, and color. 该结构表示家庭成员的得分,并带有 id、姓名、得分和颜色属性。 Step 3: Implementing the Color Helper 步骤 3:实施色彩助手 To manage colors for each family member, create a ColourHelper.swift file: 要管理每个家庭成员的颜色,请创建一个 ColourHelper.swift 文件:

// // ColourHelper.swift // FamilyScore // // Created by william on 2024/10/23. //

import Foundation import SwiftUI

enum ColourHelper: String, Codable, CaseIterable { case red case green case blue case yellow case purple case orange

var color: Color {
    switch self {
    case .red:
        return .red
    case .green:
        return .green
    case .blue:
        return .blue
    case .yellow:
        return .yellow
    case .purple:
        return .purple
    case .orange:
        return .orange
    }
}

} This enum provides a set of colors and a convenient way to convert them to SwiftUI Color objects. 此枚举提供了一组颜色以及将它们转换为 SwiftUI 颜色对象的便捷方法。

Preview: first 50% is visible. Unlock to read the full article.
To view this content, you must be a member of CodeWithWilliamJiamin's Patreon at $3 or more
Already a qualifying Patreon member? Refresh to access this content.