Skip to content

Instantly share code, notes, and snippets.

@licvido
Last active August 29, 2015 14:15
Show Gist options
  • Save licvido/b25743d422078f1492a0 to your computer and use it in GitHub Desktop.
Save licvido/b25743d422078f1492a0 to your computer and use it in GitHub Desktop.
SWIFT: Next ID for CoreData row
var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context = appDelegate.managedObjectContext
var request = NSFetchRequest(entityName: "Entity")
request.fetchLimit = 1
request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: false)]
var error: NSError? = nil
let entities = context.executeFetchRequest(request, error: &error) as [Entity]
var nextId = entities.isEmpty ? 1 : (entities[0].id + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment