简体   繁体   English

如何跟踪和保存传递到我的视图 SwiftUI 中的数据

[英]how to track and save the data passed into my view SwiftUI

I am writing a todo list app and Here is my code :我正在编写一个待办事项列表应用程序,这是我的代码:

struct TaskItem: View {
@State var task : Task
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@EnvironmentObject var taskData: UserData
@ObservedObject private var keyboard = KeyboardResponder()
var body: some View {
    Form{
        Section(header: Text("Details").font(.headline)){
            HStack{

                TextField("Title", text: $task.title ).font(Font.headline)

            }
            TextField("Description", text: $task.description)
                .font(Font.body)
        }
        Section{
            Toggle("Mark as Done", isOn: $task.isDone)
        }
        Section{
            Picker(selection: $task.priority, label: Text("priority")) {
                Text("very important").tag(2)
                Text("important").tag(1)
                Text("need to do").tag(0)
            }.pickerStyle(SegmentedPickerStyle()).padding(5)
        }
    }
    .padding(.bottom, keyboard.currentHeight)
    .edgesIgnoringSafeArea(.bottom)
    .animation(.easeOut(duration: 0.16))
    .navigationBarItems(trailing: Button(action: {
        //save data
        var result :[Task]
        result = save(id: self.task.id,
                      creationDate: self.task.creationDate,
                      creationDateYear: self.task.creationDateYear,
                      creationDateMonth: self.task.creationDateMonth,
                      creationDateDay: self.task.creationDateDay,
                      dueDate: self.task.dueDate,
                      time: self.task.time,
                      title: self.task.title,
                      description: self.task.description,
                      priority: self.task.priority,
                      isDone: self.task.isDone, taskData: self.taskData.taskData)
        self.taskData.taskData = result
        self.presentationMode.wrappedValue.dismiss()
    }, label:{
        Text("save")
    }))
    // here I put my save code
}

} }

When the save button is pushed, task's var is saved.当按下保存按钮时,任务的变量被保存。 I want to remove the save button and save data automatically once a value is changed.我想删除保存按钮并在更改值后自动保存数据。 When I move the block of // save data code out of the save button function and into var body , I get "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type" Error.当我将 // save data 代码块移出保存按钮函数并移入 var body 时,我得到“函数声明了一个不透明的返回类型,但其主体中没有可以从中推断出基础类型的返回语句”错误。

enter code here

在此处输入图片说明

var body: some View {
    Form {
    .....
    }
}

Is what you have now.是你现在拥有的。 And this is exactly, what is expected!这正是预期的结果!

Form {
    .....
}

is nothing else, just constructor of SwiftUI.Form没有别的,只是SwiftUI.Form 的构造函数

The return statement could be omitted, because it is only one expression . return语句可以省略,因为它只是一个表达式

var body: some View {
    let somtething = ....
    Form {
        ....
    }
}

is wrong.是错的。 Why?为什么? Error message explains it very clearly.错误消息非常清楚地解释了它。 " Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type " 函数声明了一个不透明的返回类型,但在其主体中没有返回语句来推断底层类型

This part of error message " Function declares " is a little bit unclear, till you take in the account, what is the difference between function and closure in Swift.这部分错误信息“ Function Declarations ”有点不清楚,直到你记下,Swift中的函数和闭包有什么区别。

var body: some View {
    ....
}

could be rewritten as func statement可以重写为 func 语句

func body()-> some View {
    ...
}

Maybe, some day, the error messages from compiler will be more clear ...也许有一天,编译器的错误信息会更清楚......

If you really like, you can do如果你真的喜欢,你可以做

var body: some View {
    let somtething = ....
    return Form {
        ....
    }
}

I am better to avoid that.我最好避免这种情况。 For better readability and easy to maintain you code, put all the logic to your model.为了更好的可读性和易于维护您的代码,请将所有逻辑放入您的模型中。 Use SwiftUI as it was designed for and take an advantage of its declarative syntax.使用 SwiftUI,因为它是专为并利用其声明性语法而设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM