gpt4 book ai didi

Extra arguments at position(位置处的额外参数)

转载 作者:bug小助手 更新时间:2023-10-26 21:05:27 25 4
gpt4 key购买 nike



I have a struct and I keep getting a extra arguments at positions error after I have added a decoder.I get the error on the return line. Any help would be appreciated. This issue only started happening as soon as I added the decoder to the struct so I am guesing now when I try to create a new instance of an Absence I need to do it slightly differently? I am quite new to SwiftUI Here is the code:

我有一个结构体,在我添加了一个解码器之后,我一直在位置错误处得到一个额外的参数。我在返回行上得到错误。任何帮助将不胜感激。这个问题只是在我将解码器添加到结构体后才开始发生,所以我猜现在当我尝试创建一个Absence的新实例时,我需要稍微改变一下?我是SwiftUI的新手,下面是代码:


struct Absence: Codable, Identifiable, Hashable {
var id: Int
var userId: Int
var start: Date
var end: Date
var active: Bool
var modifiedBy: Int
var modified: Date

// Custom initializer to handle date conversion
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
userId = try container.decode(Int.self, forKey: .userId)
active = try container.decode(Bool.self, forKey: .active)
modifiedBy = try container.decode(Int.self, forKey: .modifiedBy)

// Custom date decoding
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy" // Modify the date format as needed

if let startDateString = try? container.decode(String.self, forKey: .start),
let startDate = dateFormatter.date(from: startDateString) {
start = startDate
} else {
start = Date() // Provide a default date if decoding fails
}

if let endDateString = try? container.decode(String.self, forKey: .end),
let endDate = dateFormatter.date(from: endDateString) {
end = endDate
} else {
end = Date() // Provide a default date if decoding fails
}

if let modifiedString = try? container.decode(String.self, forKey: .modified),
let modifiedDate = dateFormatter.date(from: modifiedString) {
modified = modifiedDate
} else {
modified = Date() // Provide a default date if decoding fails
}
}

static func sampleData() -> Absence {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy" // Modify the date format as needed

let startString = "01/01/2021"
let endString = "01/02/2021"
let modifiedString = "01/03/2021" // Change this to the appropriate date string

let startDate = dateFormatter.date(from: startString) ?? Date()
let endDate = dateFormatter.date(from: endString) ?? Date()
let modifiedDate = dateFormatter.date(from: modifiedString) ?? Date()

return Absence(id: 1, userId: 1, start: startDate, end: endDate, active: true, modifiedBy: 1, modified: modifiedDate)
}}
```

更多回答
优秀答案推荐

Currently your init(...) only takes a single Decoder parameter, but you are calling Absence(id: 1, ...)
with many more parameters, that is the reason for the error.

当前您的初始化(...)只接受单个解码器参数,但您调用的是缺勤(id:1,...)对于更多的参数,这就是错误的原因。


So add another init(...) with the parameters you desire, such as:

因此,添加另一个init(...)具有您所需的参数,例如:


 init(id: Int, userId: Int, start: Date, end: Date, active: Bool, modifiedBy: Int, modified: Date) {
self.id = id
self.userId = userId
self.start = start
self.end = end
self.active = active
self.modifiedBy = modifiedBy
self.modified = modified
}

Note that, if you did not have init(from decoder: Decoder) ... you would automatically
get a memberwise initializer, if you have not defined any other custom initializers. See also the docs

请注意,如果您没有init(来自decoder:Decoder).如果你没有定义任何其他的自定义初始化器,你会自动得到一个memberwise初始化器。另见文档


更多回答

did my answer not work for you?

我的回答对你不起作用吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com