我有一个SettingsViewController
并创建了一个结构来定义每个设置所需的信息。为什么这个闭包没有我期望的论点?
///Represents a list of the options needed to populate a `SettingsTableViewCell`
public struct Setting {
///The image to use for the icon
var imageName : String
///The text to display as the title of the setting
var title : String
///Called when the switch is tapped. If no closure is supplied, the switch is hidden
var switchCallback: ((_ status: Bool)->())?
}
视图控制器保留这些设置的数组,以便稍后在表视图中使用。一个的例子下面提供:
let options : [Setting] =
[
Setting(imageName: "notifications", title: "Bump Notifications") {updateNotificationSetting($0)},
...
]
然而,当我尝试编译,我提出与错误:
Cannot convert value of type '(SettingsViewController) -> (Bool) ->()' to expected argument type '((Bool) ->())?'
有人能解释其中(SettingsViewController)
是从哪里来的吗?如果可以的话,我需要改变以修复它吗?
对于SSCCE,见下图:
import UIKit
///Represents a list of the options needed to populate a `SettingsTableViewCell`
public struct Setting {
///The image to use for the icon
var imageName : String
///The text to display as the title of the setting
var title : String
///Called when the switch is tapped. If no closure is supplied, the switch is hidden
var switchCallback: ((_ status: Bool)->())?
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let options : [Setting] =
[
//Error is on the following line
Setting(imageName: "notifications", title: "Bump Notifications") {isOn in updateSetting(isOn)},
]
func updateSetting(isOn : Bool) {
}
}
在你的闭包初始化的某处。 – Sulthan
闭包中没有明确的'self'(见上面),但我认为'updateNotifications($ 0)'前面有一个隐含的'self'。有关如何在这种情况下调用该函数的任何建议,请? –
不相关,但在Swift 3中,回调闭包中的所有参数标签已被删除。回调签名 - 正如错误信息中显示的 - 只是'(Bool) - >()','status'完全不用。 – vadian