gpt4 book ai didi

ios - Swift 2.1 Core Data - 保存具有一对多关系的数据,但如果已经存在则不添加 isome 数据

转载 作者:行者123 更新时间:2023-11-28 06:45:59 25 4
gpt4 key购买 nike

我有一个创建新食谱数据的 ViewController。要创建新食谱,用户需要填写标题、配料等并选择其类别。为此,我建立了具有一对多关系的类别和食谱实体。

在创建函数中,我需要进行以下搜索以查看类别实体是否已经具有 selectedCategory 的值。

如果 selectedCategory 已经存在于 Category 实体中,我需要找到它的索引并将该类别分配给 Recipe Entity 但我不知道我需要在这里编写什么代码。

for category in categories {
if category == selectedCategory {

/* if selectedCategory already exists in Category entity,
find its index and assign that category to Recipe entity.*/

} else {
category.name = selectedCategory
recipe.category = category
}
}

context.insertObject(recipe)

do {
try context.save()
} catch {
print("Could not save recipe")
}

最佳答案

我创建了一个支持 Core Data 的单 View 应用。

我向数据模型添加了两个实体:

CategoryEntity 具有属性“名称”。RecipeEntity 具有属性“title”。

CategoryEntity 与 RecipeEntity 有一个可选的多对一关系,称为“食谱”。它有一个级联删除规则(如果你删除一个类别,所有相关的食谱也会被删除)。

RecipeEntity 与称为“类别”的 CategoryEntity 具有必需的一对一关系。

这些关系彼此相反。

以下代码添加了一个食谱。它首先获取或创建类别,然后将配方分配给它。

import UIKit
import CoreData

class ViewController: UIViewController {

@IBOutlet weak var category: UITextField!
@IBOutlet weak var recipeTitle: UITextField!

@IBAction func createRecipe(sender: AnyObject) {

// Apple's out of the box Core Data app exposes a managedObjectContext on the appDelegate, so we'll use it here
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {

// we have to have a category name ...
if let categoryName = category.text {

// ... and a recipe title
if let recipeTitle = recipeTitle.text {

// get the category to associate this recipe with
let categoryEntity = obtainCategoryEntity(categoryName, context: appDelegate.managedObjectContext)

// create the recipe entity
let recipeEntity = (NSEntityDescription.insertNewObjectForEntityForName("RecipeEntity", inManagedObjectContext: appDelegate.managedObjectContext) as! RecipeEntity)

// assign the recipe to the category
// note that you set up relationships in Core Data by assigning entities,
// not indexes or id fields
recipeEntity.category = categoryEntity

// set the recipe's title
recipeEntity.title = recipeTitle

// save it!
do {
try appDelegate.managedObjectContext.save()
NSLog("saved context")
} catch let error as NSError {
NSLog("failed to save context with error \(error)")
} catch {
fatalError()
}
}
}
}
}

func obtainCategoryEntity(name: String, context: NSManagedObjectContext) -> CategoryEntity {

// this function gets or creates a category entity
let fetchRequest = NSFetchRequest(entityName: "CategoryEntity")
fetchRequest.predicate = NSPredicate(format: "name == %@", name)

//find existing category, if it exists
if let results = (try? context.executeFetchRequest(fetchRequest)) as? [CategoryEntity] {
if results.count > 0 {
// we really should only ever have one match, so return it
return results[0]
}
}

//category did not exist, so create it
let categoryEntity = NSEntityDescription.insertNewObjectForEntityForName("CategoryEntity", inManagedObjectContext: context) as! CategoryEntity

// set the category name
categoryEntity.name = name

// return it, but don't save it here - let the save later take care of it
return categoryEntity
}
}

关于ios - Swift 2.1 Core Data - 保存具有一对多关系的数据,但如果已经存在则不添加 isome 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346733/

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