2

I'm using CoreData in a Swift project.

I have a current user in my database, and I want to update his birthdate. By default, his birthdate is not register: in CoreData, "optional" is checked for this attribute.

I have some trouble to test if this birthdate value is nil or not. After retrieving the user, I tried to test this attribute.

I tried this following code

if user!.birthdate.isEqual(nil) {
    println("Birthdate is nil")
}

and this one

if user!.birthdate == nil {
    println("Birthdate is nil")
}

The first one is always wrong, and the second one cannot compile.

However, if I tried to print the description of the value just before, I've seen this in the log:

Printing description of $R0:
<nil>

But my variable view show me a different value:

enter image description here

So, I'm little bit lost between theses informations. Does anyone know how can I test an empty NSDate from CoreData in Swift?

Thanks,

Ben

EDIT

I've also tried this code from an other question

if let birthdate = user!.birthdate{
    // my code here
} else {
    println("Birthday nil")
}

It doesn't compile, because "Bound value in a conditional binding must be Optional type". My attribute is Optional for CoreData, but not in my NSManagedObject file generated.

Ben
  • 1,414
  • 13
  • 23
  • 1
    Re your edit: The solution to the question that I linked to required that you *edit* the generated NSManagedObject file manually. I have updated the answer with a (possibly better) solution that does not require this editing anymore. Let me know if that worked for you! – Martin R Nov 13 '14 at 15:21
  • Even if I found a solution before, it doesn't work anymore on last XCode 6.3.1. Now, I had to make my birthdate optional `@NSManaged var birthdate: NSDate?` and / or init with a default value when I init my NSManagedObject. I tried the both and it works fine for now, but I'm not really okay with these solutions. – Ben Apr 23 '15 at 09:49
  • Yes, unfortunately my proposed solution does not work anymore, I already had updated the answer accordingly. Editing the property to be an optional seems to be the only solution now. This is not satisfying, I know. You might consider to file a bug report at Apple. – Martin R Apr 23 '15 at 09:54

0 Answers0