gpt4 book ai didi

swift - 带有hashValue的“Expression was too complex to be solved in reasonable time…”

转载 作者:行者123 更新时间:2023-12-02 11:13:07 24 4
gpt4 key购买 nike

试图使我的整个数据模型可哈希化,例如...

let id:Int
let projectID:Int
let parentID:Int
let name:String
let description:String
let url:String
let startOn:Date?
let startedOn:Date?
let dueOn:Date?
let completedOn:Date?
let isStarted:Bool
let isCompleted:Bool

var hashValue:Int
{
return (31 &* id.hashValue)
&+ projectID.hashValue
&+ parentID.hashValue
&+ name.hashValue
&+ description.hashValue
&+ url.hashValue
&+ (startOn != nil ? startOn!.hashValue: 0)
&+ (startedOn != nil ? startedOn!.hashValue: 0)
&+ (dueOn != nil ? dueOn!.hashValue: 0)
&+ (completedOn != nil ? completedOn!.hashValue: 0)
&+ isStarted.hashValue
&+ isCompleted.hashValue
}

我得到编译错误:

Error:(112, 5) expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions



因此,出现了一个问题:在哈希取决于许多属性的情况下,如何使对象可哈希化?我有一些模型对象,其数量是上述属性的3-4倍。

最佳答案

将您的表情分成多部分,并通过添加下一个添加项来添加所有这些表达

var hashValue:Int
{
let firstFrg = (31 &* id.hashValue)
&+ projectID.hashValue
&+ parentID.hashValue
&+ name.hashValue
let secondFrg = firstFrg + &+ description.hashValue
&+ url.hashValue
&+ (startOn != nil ? startOn!.hashValue: 0)
let thirdFrg = secondFrg + &+ (startedOn != nil ? startedOn!.hashValue: 0)
&+ (dueOn != nil ? dueOn!.hashValue: 0)
&+ (completedOn != nil ? completedOn!.hashValue: 0)
let fourthFrg = thirdFrg + &+ isStarted.hashValue
&+ isCompleted.hashValue

return fourthFrg
}

关于swift - 带有hashValue的“Expression was too complex to be solved in reasonable time…”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686979/

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