Created
May 25, 2016 08:51
-
-
Save Sajjon/046135e51c98bdc6d2c46b6f8bb96610 to your computer and use it in GitHub Desktop.
Would be nice if Defer could expose return value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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