Last active
August 29, 2015 14:15
-
-
Save licvido/b25743d422078f1492a0 to your computer and use it in GitHub Desktop.
SWIFT: Next ID for CoreData row
This file contains hidden or 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
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