diff --git a/.DS_Store b/.DS_Store index e7a9d92..9d48cbe 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/wake.xcodeproj/project.pbxproj b/wake.xcodeproj/project.pbxproj index b8dfd9a..746286c 100644 --- a/wake.xcodeproj/project.pbxproj +++ b/wake.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ AB8773632E4E04400071CB53 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = AB8773622E4E040E0071CB53 /* .gitignore */; }; - ABB4E2182E4B761400660198 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = ABB4E2172E4B761400660198 /* Alamofire */; }; + ABE8998E2E533A7100CD7BA6 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = ABE8998D2E533A7100CD7BA6 /* Alamofire */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -27,6 +27,7 @@ /* Begin PBXFileReference section */ AB8773622E4E040E0071CB53 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = ""; }; ABB4E2082E4B75D900660198 /* wake.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = wake.app; sourceTree = BUILT_PRODUCTS_DIR; }; + ABE899102E5328F100CD7BA6 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ @@ -55,7 +56,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - ABB4E2182E4B761400660198 /* Alamofire in Frameworks */, + ABE8998E2E533A7100CD7BA6 /* Alamofire in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -67,6 +68,7 @@ children = ( AB8773622E4E040E0071CB53 /* .gitignore */, ABB4E20A2E4B75D900660198 /* wake */, + ABE8990F2E5328F100CD7BA6 /* Frameworks */, ABB4E2092E4B75D900660198 /* Products */, ); sourceTree = ""; @@ -79,6 +81,14 @@ name = Products; sourceTree = ""; }; + ABE8990F2E5328F100CD7BA6 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ABE899102E5328F100CD7BA6 /* CoreData.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -100,7 +110,7 @@ ); name = wake; packageProductDependencies = ( - ABB4E2172E4B761400660198 /* Alamofire */, + ABE8998D2E533A7100CD7BA6 /* Alamofire */, ); productName = wake; productReference = ABB4E2082E4B75D900660198 /* wake.app */; @@ -131,7 +141,7 @@ mainGroup = ABB4E1FF2E4B75D900660198; minimizedProjectReferenceProxies = 1; packageReferences = ( - ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */, + ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */, ); preferredProjectObjectVersion = 77; productRefGroup = ABB4E2092E4B75D900660198 /* Products */; @@ -372,7 +382,7 @@ /* End XCConfigurationList section */ /* Begin XCRemoteSwiftPackageReference section */ - ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */ = { + ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */ = { isa = XCRemoteSwiftPackageReference; repositoryURL = "https://github.com/Alamofire/Alamofire.git"; requirement = { @@ -383,9 +393,9 @@ /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ - ABB4E2172E4B761400660198 /* Alamofire */ = { + ABE8998D2E533A7100CD7BA6 /* Alamofire */ = { isa = XCSwiftPackageProductDependency; - package = ABB4E2162E4B761400660198 /* XCRemoteSwiftPackageReference "Alamofire" */; + package = ABE8998C2E533A7100CD7BA6 /* XCRemoteSwiftPackageReference "Alamofire" */; productName = Alamofire; }; /* End XCSwiftPackageProductDependency section */ diff --git a/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate b/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate index 51b3549..ec2f791 100644 Binary files a/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate and b/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/wake.xcodeproj/xcuserdata/elliwood.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/wake.xcodeproj/xcuserdata/elliwood.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 7b5cc31..6e69234 100644 --- a/wake.xcodeproj/xcuserdata/elliwood.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/wake.xcodeproj/xcuserdata/elliwood.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -14,8 +14,8 @@ filePath = "wake/ContentView.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "30" - endingLineNumber = "30" + startingLineNumber = "37" + endingLineNumber = "37" landmarkName = "body" landmarkType = "24"> diff --git a/wake/.DS_Store b/wake/.DS_Store index f26cef2..175eddc 100644 Binary files a/wake/.DS_Store and b/wake/.DS_Store differ diff --git a/wake/ContentView.swift b/wake/ContentView.swift index 13de58b..976c126 100644 --- a/wake/ContentView.swift +++ b/wake/ContentView.swift @@ -1,4 +1,5 @@ import SwiftUI +import SwiftData // MARK: - 自定义过渡动画 extension AnyTransition { @@ -19,6 +20,12 @@ struct ContentView: View { @State private var contentOffset: CGFloat = 0 // 内容偏移量 @State private var showLogin = false + // 获取模型上下文 + @Environment(\.modelContext) private var modelContext + + // 查询数据 - 简单查询 + @Query private var login: [Login] + // MARK: - 主体视图 var body: some View { NavigationView { @@ -40,6 +47,28 @@ struct ContentView: View { Spacer() + Text("Wake") + .font(.largeTitle) + .fontWeight(.bold) + .onTapGesture { + if login.isEmpty { + print("⚠️ 没有登录记录,正在创建新记录...") + let newLogin = Login( + email: "jyq@example.com", + name: "New User" + ) + modelContext.insert(newLogin) + try? modelContext.save() + print("✅ 已创建新登录记录") + } else if let firstLogin = login.first { + // 2. 更新现有记录 + print("🔍 找到现有记录,正在更新...") + firstLogin.email = "updated@example.com" + firstLogin.name = "Updated Name" + try? modelContext.save() + print("✅ 记录已更新") + } + } // 登录按钮 NavigationLink(destination: LoginView()) { Text("登录") @@ -160,7 +189,15 @@ struct ContentView: View { /// 显示用户资料弹窗 private func showUserProfile() { withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) { - showModal.toggle() + // print("登录记录数量: \(login.count)") + // for (index, item) in login.enumerated() { + // print("记录 \(index + 1): 邮箱=\(item.email), 姓名=\(item.name)") + // } + print("当前登录记录:") + for (index, item) in login.enumerated() { + print("记录 \(index + 1): 邮箱=\(item.email), 姓名=\(item.name)") + } +// showModal.toggle() } } @@ -182,4 +219,4 @@ struct ContentView: View { // MARK: - 预览 #Preview { ContentView() -} \ No newline at end of file +} diff --git a/wake/CoreData/Wake.xcdatamodeld/Wake.xcdatamodel/contents b/wake/CoreData/Wake.xcdatamodeld/Wake.xcdatamodel/contents new file mode 100644 index 0000000..4f2d979 --- /dev/null +++ b/wake/CoreData/Wake.xcdatamodeld/Wake.xcdatamodel/contents @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/wake/SwiftData/LoginModel.swift b/wake/SwiftData/LoginModel.swift new file mode 100644 index 0000000..29a0820 --- /dev/null +++ b/wake/SwiftData/LoginModel.swift @@ -0,0 +1,22 @@ +import Foundation +import SwiftData + +@Model +final class Login { + @Attribute(.unique) // 添加唯一性约束 + + // 基本属性 + // fulname + var name: String + // 邮箱 + var email: String + // 描述 + var description: String { + return "Login(name: \(name), email: \(email))" + } + // 初始化方法 + init(email:String="", name: String = "") { + self.email = email + self.name = name + } +} diff --git a/wake/WakeApp.swift b/wake/WakeApp.swift index 5156fec..49addd2 100644 --- a/wake/WakeApp.swift +++ b/wake/WakeApp.swift @@ -1,12 +1,7 @@ -// -// WakeApp.swift -// Wake -// -// Created by elliwood on 2025/8/11. -// import SwiftUI import UIKit +import SwiftData @main struct WakeApp: App { @@ -20,6 +15,25 @@ struct WakeApp: App { // } // } // } + // 使用更简单的方式创建模型容器 + let container: ModelContainer + + init() { + do { + // 1. 先尝试正常加载 + container = try ModelContainer(for: Login.self) + } catch { + // 2. 如果失败,尝试删除旧的存储文件 + let url = URL.applicationSupportDirectory.appending(path: "default.store") + if FileManager.default.fileExists(atPath: url.path) { + try? FileManager.default.removeItem(at: url) + } + + // 3. 重新创建容器 + container = try! ModelContainer(for: Login.self) + } + } + var body: some Scene { WindowGroup { @@ -37,5 +51,7 @@ struct WakeApp: App { // } // } } + // 注入模型容器到环境中 + .modelContainer(container) } }