gpt4 book ai didi

ios - 无法在 swift 2 中更新实体

转载 作者:行者123 更新时间:2023-11-29 01:12:46 26 4
gpt4 key购买 nike

我在使用 swift 2 更新实体记录时遇到问题,在我的代码中,我希望能够从选择器 View 中选择一个值,例如语言,然后将该值存储到核心数据中,但如果它在更新该值之前已存储。

我的所有代码都有效,但我的更新代码给我应用程序委托(delegate)错误: enter image description here

这是我的完整代码:

import UIKit
import CoreData

class secondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

//Mark : initializing variables
@IBOutlet var labelPassedData: UILabel!
@IBOutlet var pickerView: UIPickerView!
var toPass:String!

//data source
var languageDB = [NSManagedObject]()
@IBOutlet weak var dbshow: UILabel!

let managedContext = AppDelegate().managedObjectContext

var languagePicker = ["فارسی","English","العربية"]



override func viewDidLoad() {
super.viewDidLoad()
pickerView = UIPickerView()

pickerView.dataSource = self
pickerView.delegate = self

labelPassedData.text = languagePicker[0] //

fetch("Language")
let counter : Int = languageDB.count
print (counter)
let Language = languageDB[counter - 1]
dbshow!.text = Language.valueForKey("lang") as? String

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func numberOfComponentsInPickerView (pickerView : UIPickerView) -> Int{
return 1
}

func pickerView(pickerView : UIPickerView , numberOfRowsInComponent component : Int) ->Int{
return languagePicker.count
}

func pickerView(pickerView : UIPickerView, titleForRow row:Int, forComponent component : Int) -> String?
{
return languagePicker[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){

labelPassedData.text = languagePicker[row]

self.Insert(labelPassedData!.text!) // calling save lang function


let Language = languageDB[row]
dbshow!.text = Language.valueForKey("lang") as? String

self.view.endEditing(true)
}



func Insert(lang : String) {
let check_empty = entityIsEmpty("Language")
if(check_empty != true){

let saveLanguage = NSEntityDescription.insertNewObjectForEntityForName("Language", inManagedObjectContext: managedContext)

// add our data
saveLanguage.setValue(lang, forKey: "lang")

//2
do {
try managedContext.save()
//3
languageDB.append(saveLanguage)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}

//check_empty
}else{

print ("update")
let predicate = NSPredicate(format: "objectID == %@", "31")

let fetchRequest = NSFetchRequest(entityName: "Language")
fetchRequest.predicate = predicate

do {
let fetchedEntities = try self.managedContext.executeFetchRequest(fetchRequest) as! [Language]
fetchedEntities.first?.lang = lang

// ... Update additional properties with new values
} catch {
// Do something in response to error condition
}

do {
try self.managedContext.save()
} catch {
// Do something in response to error condition
}

}
}


func fetch(entity: String){
let langfetch = NSFetchRequest(entityName: entity)

do{
let results = try managedContext.executeFetchRequest(langfetch)
languageDB = results as! [NSManagedObject]

}catch{
fatalError("bad things happend \(error)")
}

}


func entityIsEmpty(entity: String) -> Bool
{

let request = NSFetchRequest(entityName: entity)

do{
let results = try managedContext.executeFetchRequest(request)
languageDB = results as! [NSManagedObject]

}catch{
fatalError("Cant handel entity \(error)")
}

if languageDB.count != 0
{
return true
}
else
{
return false
}


}



}

最佳答案

大概是这一行:

let fetchRequest = NSFetchRequest(entityName: "MyEntity")

应该使用“语言”作为实体名称。我怀疑这是错误的根源。但我也认为你的谓词会导致问题——看起来你只想保存一个项目,所以不需要过滤结果——所以不需要谓词。

关于ios - 无法在 swift 2 中更新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35558775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com