TheEwook TheEwook - 2 months ago 20
Swift Question

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.

Answer

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?
Comments