Answers for "swift core data order by date"

1

swift core data sort by date

var sdSortDate = NSSortDescriptor.init(key: "date", ascending: false)
fr.sortDescriptors = [sdSortDate]
let result4 = try! context.fetch(fr)
Posted by: Guest on April-23-2020
0

swift core data order by

func sortlist(){

    //1
    guard let appDelegate =
        UIApplication.shared.delegate as? AppDelegate else {
            return
    }

    let managedContext =
        appDelegate.persistentContainer.viewContext

    //2
    let fetchRequest =
        NSFetchRequest<NSManagedObject>(entityName: "User")
    let sort = NSSortDescriptor(key: "date", ascending: false)
    fetchRequest.sortDescriptors = [sort]

    //3
    do {
        let  langugeCodes = try managedContext.fetch(fetchRequest)
        for result in langugeCodes as [NSManagedObject] {
            var   username:String = result.value(forKey: "username")! as! String

            print("username==>",username)


        }


    } catch let error as NSError {
        print("Could not fetch. (error), (error.userInfo)")
    }
}
Posted by: Guest on May-11-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language