sort coreData fetch(swift4)

时间:2017-11-28 20:47:27

标签: ios class sorting core-data nspredicate

现在我的代码使用NSPredicate或排序描述符基本上只是收集一个名字。我希望NSPredicate能够对a-z中的所有名称进行排序。

COREDATAHANDLER

class coreDataHandler: NSObject {

private class func getContext() -> NSManagedObjectContext{

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext
}
class func saveObject(username:String, password:String) -> Bool{
 let context = getContext()
    let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
    let managedObject = NSManagedObject(entity: entity!, insertInto: context)

    managedObject.setValue(username, forKey: "username")
  managedObject.setValue(password, forKey: "password")
    do {
        try context.save()
        return true
    } catch {
        return false}



    }
 class func filterData() -> [User]? {
    let conttext = getContext()
    let fetchRequest:NSFetchRequest<User> = User.fetchRequest()

    var user:[User]? = nil

    var predicate = NSPredicate(format: "username contains[d] %@" ,"duke")

    fetchRequest.predicate = predicate
    do {
        user = try conttext.fetch(fetchRequest)
        return user
    }catch {
        return user
    }
}

的viewController

  user = coreDataHandler.filterData()
    for i in user! {
        print("Judou : \(i.username!)")
    }

2 个答案:

答案 0 :(得分:0)

如果您希望user数组按username然后password排序,我会在您的CoreDataHandler类中添加一个类函数以获取排序数组:

class func getSortedData() -> [User]? {
    let conttext = getContext()
    let fetchRequest:NSFetchRequest<User> = User.fetchRequest()

    var user:[User]? = nil

    var nameSort = NSSortDescriptor(key:"username", ascending:true)            
    var passwordSort = NSSortDescriptor(key:"password", ascending:false) 

    fetchRequest.sortDescriptors = [nameSort, passwordSort]
    do {
        user = try conttext.fetch(fetchRequest)
        return user
    } catch {
        return user
    }
}

然后在视图控制器中,使用此新函数获取已排序的数组:

user = coreDataHandler.getSortedData()
for i in user! {
    print("Judou : \(i.username!), Password:\(i.password)")
}

答案 1 :(得分:0)

在Swift 4或Swift 5中,您可以使用

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: "username", 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
            var   password:String = result.value(forKey: "password")! as! String

            print("username==>",username)
            print("name==>",name)

        }


    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }
}