Skip to content

Instantly share code, notes, and snippets.

@Sajjon
Created May 25, 2016 08:51
Show Gist options
  • Save Sajjon/046135e51c98bdc6d2c46b6f8bb96610 to your computer and use it in GitHub Desktop.
Save Sajjon/046135e51c98bdc6d2c46b6f8bb96610 to your computer and use it in GitHub Desktop.
Would be nice if Defer could expose return value
// We don't know the return value of this function! Makes it hard to debug!
func fetchUserByFirstName(firstName: String, andLastName lastName: String, fromContext context: NSManagedObjectContext) -> User? {
defer {
// Not possible, would be nice! Return value would be an implicitly declared variable
// exaclty like the variables 'newValue' and 'oldValue' in property observers!
print("return value: \(returnValue)")
}
guard !firstName.isEmpty else { print("firstName can't be empty"); return nil }
guard !lastName.isEmpty else { print("lastName can't be empty"); return nil }
guard firstName != "DEBUG" else { return User.debugUser }
let fetchRequest = NSFetchRequest(entityName: Users.entityName)
let predicate = NSPredicate(format: "firstName == \(firstName) AND lastName == \(lastName)")
fetchRequest.predicate = predicate
do {
let user = try context.executeFetchRequest(fetchRequest)
return user
} catch let error as NSError {
print("Error fetching user: \(error)")
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment