Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 参数类型';任何';应为类或类约束类型的实例_Ios_Swift - Fatal编程技术网

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)