Stasya
Stasya

Reputation: 61

Argument type 'Any' expected to be an instance of a class or class-constrained type

I Am trying to build a reminders app and am running into a problem.

i have a function,

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)
    }
}

and it says, Argument type 'Any' expected to be an instance of a class or class-constrained type how do I fix this?

Upvotes: 6

Views: 19620

Answers (1)

vadian
vadian

Reputation: 285069

You have to cast the unspecified sender parameter to the expected type to be able to compare it

if let button = sender as? UIButton,
  saveButton == button {

or with guard

guard let button = sender as? UIButton,
   saveButton == button else { return }

But if there is only one segue you can force unwrap the sender parameter

let button = sender as! UIButton

However in your example the sender parameter is unused anyway, so what's the comparison for?

Upvotes: 9

Related Questions