Ios 参数类型';任何';应为类或类约束类型的实例
我正在尝试构建一个提醒应用程序,但遇到了一个问题 我有一个功能Ios 参数类型';任何';应为类或类约束类型的实例,ios,swift,Ios,Swift,我正在尝试构建一个提醒应用程序,但遇到了一个问题 我有一个功能 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if saveButton === sender { let name = reminderTextField.text var time = timePicker.date let timeInterval = floor(time
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if saveButton === sender {
let name = reminderTextField.text
var time = timePicker.date
let timeInterval = floor(time.timeIntervalSinceReferenceDate/60)*60
time = NSDate(timeIntervalSinceReferenceDate: timeInterval) as Date
//build; notification
let notification = UILocalNotification()
notification.alertTitle = "Reminder"
notification.alertBody = "Don't forget to \(name!)!"
notification.fireDate = time
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
reminder = Reminder(name: name!, time: time as NSDate, notification: notification)
}
}
它说,参数类型“Any”应该是类或类约束类型的实例
如何修复此问题?您必须将未指定的
发送方
参数强制转换为期望的类型,才能对其进行比较
if let button = sender as? UIButton,
saveButton == button {
或使用guard
guard let button = sender as? UIButton,
saveButton == button else { return }
但如果只有一个序列,则可以强制展开sender参数
let button = sender as! UIButton
但是,在您的示例中,
发送方
参数仍然未使用,因此比较的目的是什么?您必须将未指定的发送方
参数转换为期望的类型,以便能够对其进行比较
if let button = sender as? UIButton,
saveButton == button {
或使用guard
guard let button = sender as? UIButton,
saveButton == button else { return }
但如果只有一个序列,则可以强制展开sender参数
let button = sender as! UIButton
但是,在您的示例中,
sender
参数仍然未使用,因此比较结果是什么?如果saveButton==(sender as?UIButton)您可以直接使用此如果saveButton==(sender as?UIButton)