swift 来自https://stackoverflow.com/questions/26345189/how-do-you-update-a-coredata-entry-that-has-alr

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 来自https://stackoverflow.com/questions/26345189/how-do-you-update-a-coredata-entry-that-has-alr相关的知识,希望对你有一定的参考价值。

func saveLoginData(accessToken: String, userName: String) {
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    var context: NSManagedObjectContext = appDel.managedObjectContext!

    var fetchRequest = NSFetchRequest(entityName: "LoginData")
    fetchRequest.predicate = NSPredicate(format: "userName = %@", userName)

    if let fetchResults = appDel.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSManagedObject] {
        if fetchResults.count != 0{

            var managedObject = fetchResults[0]
            managedObject.setValue(accessToken, forKey: "accessToken")

            context.save(nil)
        }
    }
}

以上是关于swift 来自https://stackoverflow.com/questions/26345189/how-do-you-update-a-coredata-entry-that-has-alr的主要内容,如果未能解决你的问题,请参考以下文章