英文:
How to save what users write in SwiftUI app?
问题
我想要添加保存用户所写内容的功能,这样,如果他们返回主页或关闭应用然后重新打开,他们所写的内容仍然会保留。
这是我正在处理的代码。我想知道我是否需要添加更多内容?
英文:
I want to add the ability to save what the users write, so if they go back to the homepage or close the app and re-open it, what they wrote would still be there.
Here the code I'm working with. I'm wondering if I have to add something more to it?
struct LandingView: View {
@State private var oldLandingName: String = ""
@State private var newLandingName: String = ""
@State private var isAddingLanding = false
@State private var landings: [String] = []
@State private var isEditing = false
var body: some View {
NavigationView {
ZStack {
Color.blue.ignoresSafeArea()
Circle()
.scale(1.7)
.foregroundColor(.yellow.opacity(0.60))
Circle()
.scale(1.3)
.foregroundColor(.blue.opacity(0.50))
VStack {
HStack {
Text("Add Your Travels")
.bold()
Spacer()
Button(action: {
isAddingLanding = true
}) {
Image(systemName: "plus")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.white)
}
}
.padding(.horizontal)
List(landings, id: \.self) { landing in
Text(landing).bold()
.foregroundColor(.white)
.listRowBackground(Color.clear)
.listRowSeparatorTint(.blue)
.onTapGesture {
self.isEditing = true
self.oldLandingName = landing
self.newLandingName = landing
}
}
.listStyle(.plain)
}
}
.navigationBarTitle(Text("Landings!"))
.fullScreenCover(isPresented: $isAddingLanding, onDismiss: {}) {
VStack {
TextField("Where did you go? \(newLandingName)", text: $newLandingName)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button("Add") {
if !newLandingName.isEmpty {
landings.append(newLandingName)
newLandingName = ""
}
isAddingLanding = false
}
Color.blue.ignoresSafeArea()
}
}
.fullScreenCover(isPresented: $isEditing, onDismiss: {}) {
VStack {
TextField("", text: $newLandingName)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button("Update") {
if !newLandingName.isEmpty {
if let idx = landings.firstIndex(where: { $0 == $oldLandingName.wrappedValue }) {
self.landings[idx] = newLandingName
}
}
isEditing = false
}
Color.blue.ignoresSafeArea()
}
}
}
}
}
答案1
得分: 1
以下是要翻译的内容:
User Defaults: User Defaults是一种用于存储小量数据的简单轻量级选项,例如用户偏好设置。它使用UserDefaults类以键值对的方式存储数据。
JSON: 您可以使用Foundation框架提供的API直接将数据保存到文件系统中。这包括以各种格式如文本文件、JSON或自定义二进制格式读取和写入文件。
Core Data: Core Data是由Apple提供的框架,用于管理对象图并将数据持久化到SQLite数据库或其他格式中。它提供了面向对象的方法,并支持数据验证和关系管理等高级功能。
SQLite: SQLite是一个提供轻量级嵌入式关系数据库的C库。您可以在Swift应用程序中直接使用SQLite来创建和管理本地数据库文件以存储结构化数据。
英文:
There are several ways, here are some common ones:
User Defaults: User Defaults is a simple and lightweight option for storing small amounts of data, such as user preferences or settings. It uses the UserDefaults class to store data in key-value pairs.
JSON: You can save data directly to the file system using APIs provided by Foundation framework. This includes reading and writing files in various formats such as text files, JSON, or custom binary formats.
Core Data: Core Data is a framework provided by Apple for managing the object graph and persisting data in a SQLite database or other formats. It provides an object-oriented approach and supports advanced features like data validation and relationship management.
SQLite: SQLite is a C library that provides a lightweight and embedded relational database. You can use SQLite directly in Swift applications to create and manage a local database file for storing structured data.
答案2
得分: 0
有很多方法可以做到这一点。
其中一种有效的方法是使用Core Data,并以一种使您的实体可以轻松增长且不会丧失一致性的方式保存您的信息。了解更多关于Core Data的信息。
如果您只想保存不是敏感信息的小信息片段,那么您可以使用:
@AppStorage("propertyName") private var cityName = ""
但我不建议在生产环境中使用它。
英文:
There many ways to do that.
One effective way is to use Core Data and save your information in a way that your entities can easily grow and you don't lose consistency. Get to know more about Core Data.
If you only want to save small pieces of information that are not sensitive information then you can use:
@AppStorage("propertyName") private var cityName = ""
but I wouldn't recommend it for production.
答案3
得分: 0
有多种方式可以实现iOS的数据持久化。其中两种最受欢迎的选项包括由Apple创建的Core Data和目前由MongoDB拥有的Realm Swift。
Core Data
优点:
- 由Apple创建
- 支持复杂的数据结构
- 与CloudKit更容易集成
缺点:
- 需要更多设置
- 可能会变得复杂
这是一个Core Data入门指南链接:
https://www.hackingwithswift.com/quick-start/swiftui/how-to-configure-core-data-to-work-with-swiftui
Realm Swift
优点:
- 更容易使用
- 声称更快
缺点:
- 不是由Apple开发,因此新的操作系统功能可能需要更长时间才能获得支持
这是一个Realm Swift入门指南链接:
https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/
英文:
There are multiple ways data persistence can be achieved for iOS. Two of the most popular options include Core Data, which Apple created, and Realm Swift, currently owned by MongoDB.
Core Data
Advantages:
- Created by Apple
- Supports complex data structures
- Easier integration with CloudKit
Disadvantage:
- More setup required
- Can be complex
Here's a getting-started guide for Core Data:
https://www.hackingwithswift.com/quick-start/swiftui/how-to-configure-core-data-to-work-with-swiftui
Realm Swift
Advantages:
- Easier to use
- Claim to be faster
Disadvantages:
- Not developed by Apple, so new OS features might take longer to gain support
Here's a getting-started guide for Realm Swift:
https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/
答案4
得分: 0
我正在使用Core Data进行保存。但我遇到了另一个问题,那就是在当前列表中添加导航栏。点击进入后,我可以写入数据并保存。我发现当我写入时,它们会出现在所有的列表单元格中,这非常糟糕!我希望它们是独立的。如果您能解决这个问题,请告诉我,谢谢!
英文:
I am using Core Data to save. But I encountered another problem, which is adding a navigation bar to the current list. After clicking to enter, I can write data and save it. I found that when I write, they will appear in all the list cells, which is very bad! I hope they are independent. If you can crack it, please let me know, thank you!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论