gpt4 book ai didi

swift - 如何将 Void 类型添加到元组?

转载 作者:可可西里 更新时间:2023-11-01 00:00:01 25 4
gpt4 key购买 nike

我在为元组集合声明 Void 类型时遇到了一些麻烦。我收到以下错误:Nil is not compatible with expected element type 'Void '

var messages: [(person: String, type: String, text: String, action: Void, isSender: Bool)]? = [
(person: "Buddy", type: "instruction", text: "Wat vervelend van uw schade. De volgende vragen moeten direct op locatie ingevuld worden", action: nil, isSender: false),
(person: "User", type: "question", text: "Wat is uw locatie en tijd?", action: askPermissionForGPS(), isSender: true)
]

我已经尝试将 lazy 放在字典集合之前,以及声明类型 VoidVoid -> () 的不同方式, () -> Void,但没有结果。

我想要的是元组可以有一个函数作为参数或 nil。

最佳答案

如果你想使用nil,你必须将action参数声明为可选

var messages: [(person: String, type: String, text: String, action: Void?, isSender: Bool)]? = [ ...

但我建议将action参数声明为闭包

var messages: [(person: String, type: String, text: String, action: (()->Void)?, isSender: Bool)]? = [ ...

然后你可以只传递函数名

 (person: "User", 
type: "question",
text: "Wat is uw locatie en tijd?",
action: askPermissionForGPS,
isSender: true)

关于swift - 如何将 Void 类型添加到元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053482/

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