0

I have the following ClassRoom Struct

struct ClassRoom: Codable {
  let name: String
  let classId: String
  let date: String
}

While I am forming a ClassRoom object, I do not need date object. // do not need date object here!,

let classRoom = ClassRoom(name: "Math", classId: "12343")
 Missing argument for parameter 'date' in call

But I need date object when I decode the ClassRoom.

3
  • Make it optional. Commented Apr 26, 2021 at 23:09
  • It is still complaining Missing argument for parameter 'date' in call Commented Apr 26, 2021 at 23:11
  • I think it needs to be defaulted to nil, and made mutable: var date: String? = nil. Commented Apr 26, 2021 at 23:14

1 Answer 1

3

You need to make your date be optional.

Two approaches:

You can keep using the compiler synthesized memberwise initializer. For it to use nil as a default value, you need to make the date variable mutable:

struct ClassRoom: Codable {
    let name: String
    let classId: String
    var date: String? = nil
}

If you want to keep it mutable, you'll have to add your own initializer, with a defaulted parameter, like so:

struct ClassRoom: Codable {
    let name: String
    let classId: String
    let date: String?
    
    init(name: String, classId: String, date: String? = nil) {
        self.name = name
        self.classId = classId
        self.date = date
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.