I have code to execute NSFetchRequest and output its result to an array of my custom data model type. An extraction may throw, but I don't want to take care of the error, so am I using try? , and also use as? when casting. In Swift 2, this was just fine, but Swift 3 creates a double option:
var expenses: [Expense]? { let request = NSFetchRequest<NSFetchRequestResult>(entityName: Expense.entityName) request.predicate = NSPredicate(format: "dateSpent >= %@ AND dateSpent <= %@", [self.startDate, self.endDate]) // Returns [Expense]? because right side is [Expense]?? if let expenses = try? App.mainQueueContext.fetch(request) as? [Expense], expenses?.isEmpty == false { return expenses } return nil }
How can I rephrase the right side of my optional binding in if let , so that its type is just an [Expense] array? I think it is absurd that in the following logical condition (which used to be a where clause), the array is still optional.
source share