我在为元组集合声明类型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)
]
Run Code Online (Sandbox Code Playgroud)
我已经尝试过的地方lazy字典收集和不同的方式申报类型之前Void,Void -> (),() -> Void,但没有结果.
我想要的是元组可以有一个函数作为参数或零.
如果要使用nil,则必须将action参数声明为可选参数
var messages: [(person: String, type: String, text: String, action: Void?, isSender: Bool)]? = [ ...
Run Code Online (Sandbox Code Playgroud)
但我建议将action参数声明为closure
var messages: [(person: String, type: String, text: String, action: (()->Void)?, isSender: Bool)]? = [ ...
Run Code Online (Sandbox Code Playgroud)
那么你可以只传递函数名称
(person: "User",
type: "question",
text: "Wat is uw locatie en tijd?",
action: askPermissionForGPS,
isSender: true)
Run Code Online (Sandbox Code Playgroud)