Validating the return value of executeFetchRequest() . The return value is nil if the selection is not performed, in which case the error variable will be set, so there is no need to check if let error = fetchError .
Note that the request does not work if there is no (corresponding) object. In this case, an empty array is returned.
let personRequest = NSFetchRequest(entityName: "Person") var fetchError : NSError? if let personResult = managedObjectContext.executeFetchRequest(personRequest, error: &fetchError) as? [Person] { if personResult.count == 0 { println("No person found") } else { println("Persons found: \(personResult.count)") } } else { println("fetch failed: \(fetchError!.localizedDescription)") }
source share