-1

I am a beginner with swift and I'm trying to complete my first app. While I was typing the code, it showed me this:

Cannot assign value of type 'NSDate' to type 'Date?

at

newBirthday.birthdate = birthdate as NSDate

I tried writing statements for making that line of code work but it wouldn't. Every time now I am running it, I would get 6 errors.

let newBirthday = Birthday(context: context)
newBirthday.firstName = firstName
newBirthday.lastName = lastName
newBirthday.birthdayGift = birthdayGift
newBirthday.birthdate = birthdate as NSDate
newBirthday.birthdayID = UUID().uuidString
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

1

Starting with Swift 3, it no longer used Objective-c libraries NS

So if use Swift 3.0 or greater, then remove NS Prefix

In your case Birthday Object variable 'birthdate' is Date type not NSDate

if you can not assign NSDate in the Date object, for assign NSDate in Date you have cast first in Date.

newBirthday.birthdate = birthdate
Nilesh R Patel
  • 697
  • 7
  • 17