How to define auto increment attribute in master data?

I use the main data in my Swift application and want to set the ID attribute in each object as an auto-increment. Is this possible in the master data? Or do I need to manually insert a record and control the increment programmatically?

My software attempt, where katid is my unique key:

    let fetchRequest = NSFetchRequest(entityName: "Kategorien")
    let sortDescriptor = NSSortDescriptor(key: "katid", ascending: true, selector: #selector(NSString.compare(_:)))
    fetchRequest.sortDescriptors = [sortDescriptor]


    var newID = 0
    do {
        let result = try managedObjectContext.executeFetchRequest(fetchRequest).last as! Kategorien!
        if result != nil {
            newID = (result.katid?.integerValue)! + 1
        }
    } catch let error as NSError {
        NSLog("Unresolved error \(error)")
    }

Any best solution; -)

+4
source share

All Articles