简体   繁体   English

【SwiftUI】如何初始化一个空的struct

[英]【SwiftUI】How to initialize an empty struct

I need some kind help in SwiftUI.我需要 SwiftUI 方面的帮助。

I have a model below that takes in JSON data.我在下面有一个 model 接收 JSON 数据。 When I want to initialize it as an empty array, it should be simple, just stating Answers, but how can I initialize it not as an array but an empty single data set.当我想将它初始化为一个空数组时,应该很简单,只是说明Answers,但是我怎样才能不将它初始化为一个数组而是一个空的单个数据集。

struct Answers: Identifiable, Decodable  {
    let id: Int
    let questionsUsersId: Int
    let reactions: [Reactions]

enum CodingKeys: String, CodingKey {
        case id = "questions_id"
        case questionsUsersId = "questions_users_id"
        case reactions = "reactions"
    }

init(from decoder: Decoder) throws {

    let container = try decoder.container(keyedBy: CodingKeys.self)

    self.id = try container.decodeIfPresent(Int.self, forKey: .id) ?? 0
    self.questionsUsersId = try container.decodeIfPresent(Int.self, forKey: .questionsUsersId) ?? 0
    self.reactions = try container.decodeIfPresent([Reactions].self, forKey: .reactions) ?? [Reactions]()
}


 }

I've tried var answer: Answers = Answers( ) but got the errors below:我试过 var answer: Answers = Answers() 但得到以下错误:

//Missing argument for parameter 'from' in call
//Insert 'from: <#Decoder#>'

The reason I want to initialize it empty is because I need to hand over the default value of it when the data model is nil.我想将它初始化为空的原因是因为当数据 model 为 nil 时,我需要交出它的默认值。 (ie what should I write in the xxx?) (即我应该在 xxx 中写什么?)

 @ObservedObject var receiveAnswersViewModel = ReceiveAnswersViewModel()
  //abbreviate
            NavigationLink(destination: AnswerDetailView(answer: receiveAnswersViewModel.answer ?? xxx), isActive: $shouldNavigate) {
                    
                        ForEach(myPageViewModel.answers(forFilter: selectedFilter)?.answers ?? [Answers]()) { answer in
                         
                         AnswerCell(answer: answer)
                                .gesture(TapGesture()
                                                .onEnded({ _ in
                                                    //your action here
                                    self.answersId = answer.answersId
                                    receiveAnswersViewModel.fetchAnswers(answers_id: answersId ?? 0)
                                       }))
                         }
                    }

This is what I'm trying to do above: Show several cells using NavigationLink.这就是我在上面尝试做的:使用 NavigationLink 显示几个单元格。 (Imagine that the cells are tweets.) Press one of the tweets in the list → Call fetchAnswers func in the View Model which takes the pressed tweet id as the argument → Returns the specific tweet detail info→ Put it in the Answer model → Hand it over to the View → Navigate to the Tweet Detail View. (假设单元格是推文。)按列表中的一条推文 → 在 View Model 中调用 fetchAnswers 函数,该函数以按下的推文 id 作为参数 → 返回特定的推文详细信息 → 将其放入答案 model → 手它转到视图→导航到推文详细信息视图。

View Model code is below:查看 Model 代码如下:

class ReceiveAnswersViewModel: ObservableObject {
@Published var answer: Answers?

func fetchAnswers(answers_id: Int){
//abbreviate
self.answer = try JSONDecoder().decode(Answers.self, from: data)
}

Thanks in advance.提前致谢。

A reasonable way is to add a static method to create an empty instance.一个合理的方法是添加一个 static 方法来创建一个空实例。

By the way name the struct in singular form, an instance represents likely one Answer顺便说一下以单数形式命名结构,一个实例可能代表一个Answer

struct Answer: Identifiable, Decodable  {
    let id: Int
    let questionsUsersId: Int
    let reactions: [Reactions]

    static let example = Answer(id: 0, questionsUsersId: 0, reactions: [])

...

Then you can declare然后你可以声明

var answer = Answer.example 

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

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