我有function
entity
和item
,并将值附加到array
,但它只添加了最后一项,我认为这是因为每次迭代数组时,我都会将值追加到instance
。我无法弄清楚原因。
我的功能:
func saveItem(itemToSave: String, key: String){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "Recipe", in: managedContext)
let item = NSManagedObject(entity: entity!, insertInto: managedContext)
item.setValue(itemToSave, forKey: key)
do{
try managedContext.save()
recipes.append(item)
}catch{
print("\(error)")
}}
我如何调用该函数:
if let title = self.newRecipeView.titleTextField.text{
saveItem(itemToSave: title, key: "title")
}
第一张图片:https://i.stack.imgur.com/UrLbi.png 第二张图片:https://i.stack.imgur.com/XjiEP.png
单击按钮时的操作:
UIView.animate(withDuration: 0.7, delay: 0, options: .curveEaseOut, animations: {
clearView.alpha = 1
self.contentView.isUserInteractionEnabled = false
activityIndicator.startAnimating()
}, completion: { (true) in
if let title = self.newRecipeView.titleTextField.text{
saveItem(itemToSave: title, key: "title")
}
if let category = UserDefaults.standard.object(forKey: "category") as! String?{
saveItem(itemToSave: category, key: "category")
}
if let rating = self.newRecipeView.ratingTextField.text{
saveItem(itemToSave: rating, key: "rating")
}
if let time = self.newRecipeView.timeTextField.text{
saveItem(itemToSave: time, key: "time")
}
if let directions = self.newRecipeView.directionsTextField.text{
saveItem(itemToSave: directions, key: "directions")
}
fetchData()
print(recipes)
activityIndicator.stopAnimating()
_ = self.navigationController?.popToRootViewController(animated: true)
})
保存数据的方法:
func saveItem(itemToSave: String, key: String){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "Recipe", in: managedContext)
let item = NSManagedObject(entity: entity!, insertInto: managedContext)
item.setValue(itemToSave, forKey: key)
do{
try managedContext.save()
recipes.append(item)
}catch{
print("\(error)")
}
}
获取数据的方法:
func fetchData(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Recipe")
do{
let results = try managedContext.fetch(fetchRequest)
recipes = results as! [NSManagedObject]
}catch{
print("\(error)")
}
}
答案 0 :(得分:1)
您的问题是,您为每个Recipe
创建了一个新的item
。
您需要将功能更改为以下内容:
func saveItem(title: String, rating: String, category: String, time: String, directions: String){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "Recipe", in: managedContext)
let item = NSManagedObject(entity: entity!, insertInto: managedContext)
recipe.setValue(title, forKey: "title")
recipe.setValue(rating, forKey: "rating")
recipe.setValue(category, forKey: "category")
recipe.setValue(time, forKey: "time")
recipe.setValue(directions, forKey: "directions")
do{
try managedContext.save()
recipes.append(recipe)
}catch{
print("\(error)")
}
}
然后通过以下方式调用它:
saveItem(title: self.newRecipeView.titleTextField.text, rating: self.newRecipeView.ratingTextField.text, category: (UserDefaults.standard.object(forKey: "category") as! String?), time: self.newRecipeView.timeTextField.text, directions: self.newRecipeView.directionsTextField.text)