mercredi 21 octobre 2015

if let nil in swift 2.0 is not working as expected

"if let nil" check is not working in Swift 2.0, but was working fine in Swift 1.2.

In the below code, "fetchNumbersFromCoreDataDB" function returns nil. In getDatabaseDate function, I have a condition "if let date = dbDate". This condition ideally should fail and go to else block. But in Swift 2.0, it is going inside the if block. In Swift 1.2, same code is going to else block. I am confused here. What is the difference here?

 func getDatabaseDate() {
        let dbDate : NSDate? = ((self.fetchNumbersFromCoreDataDB(“123456”) as DBNumbers!).dbDate)
        if let date = dbDate {
           print(“\(date)”)
        }
        else {
            print(“No Date”)
        }
    }


 func fetchNumbersFromCoreDataDB(Number:String) -> DBNumbers? {
      var numArray = coreDataInterface.fetchAllObjectsFromEntity(info, withAttributeValues: [“No” : Number]) as! [DBNumbers]
      return numArray.count>0 ? numArray[0] : nil
 }

Note: "dbDate" is NSDate type in Coredata table

Aucun commentaire:

Enregistrer un commentaire