TheEwook
TheEwook

Reputation: 11117

iOS 10 Xcode 8 - migration to Swift 3 and Date

I'm in the process of migration an iOS app from swift 2 to swift 3 and I encounter this error that I don't understand and I'm not sure what to do.

The issue occurs when I try to read the property called dateApproved but not for dateCreated.

print("dateApproved: \(cEntity.dateApproved)")
print("dateCreated: \(cEntity.dateCreated)")

Entity class

@objc(entity)
open class Entity: NSManagedObject {

    ...
    @NSManaged var dateApproved: Date
    @NSManaged var dateCreated: Date
    ...

Upon inspecting the property cEntity I can see that

dateApproved = nil;
dateCreated = "2016-08-24 22:20:38 +0000";

This is a screenshot of the error

enter image description here

Note: it worked before, it just doesn't work anymore since I'm migrated all the code to make it compliant with Swift 3.

Could you please give me some pointers about how to solve/track this issue. Very much appreciated.

Upvotes: 1

Views: 208

Answers (1)

Codo
Codo

Reputation: 78845

The instance variable dateApproved is declared as a non-optional variable, i.e. Swift will assume it's never nil. However, as the debugger shows, it is nil.

Therfore, change the declaration to match reality and make it optional.

@NSManaged var dateApproved: Date?

Upvotes: 1

Related Questions