创建CoreData关系会导致代码中出现20多个错误

时间:2017-11-15 15:26:13

标签: ios swift core-data

因此,过去几周我一直在尝试使用关系,以便将一个对象数组ingredient添加到recipe的单个类中。我一直在看this video。现在,通过创建recipeingredient类的两个扩展程序,我的代码中出现了大量错误,特别是说

  

无效使用(类结构)

  

模糊使用(类结构)

我在GitHub上有一个Git Repo,如果可以的话,你可以诊断我的代码here

如果没有,这是我所做的扩展

食谱类:

import Foundation
import CoreData


extension Recipe {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Recipe> {
    return NSFetchRequest<Recipe>(entityName: "Recipe")
}

@NSManaged public var ingredients: String?
@NSManaged public var instructions: String?
@NSManaged public var time: String?
@NSManaged public var title: String?
@NSManaged public var ingredient: [Ingredient]?

}

// MARK: Generated accessors for ingredient
extension Recipe {

@objc(addIngredientObject:)
@NSManaged public func addToIngredient(_ value: Ingredient)

@objc(removeIngredientObject:)
@NSManaged public func removeFromIngredient(_ value: Ingredient)

@objc(addIngredient:)
@NSManaged public func addToIngredient(_ values: NSSet)

@objc(removeIngredient:)
@NSManaged public func removeFromIngredient(_ values: NSSet)

}

成分类:

import Foundation
import CoreData


extension Ingredient {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Ingredient>{
    return NSFetchRequest<Ingredient>(entityName: "Ingredient")
}

@NSManaged public var cost: Double
@NSManaged public var name: String?
@NSManaged public var unit: String?
@NSManaged public var recipe: NSSet?

}

// MARK: Generated accessors for recipe
extension Ingredient {

@objc(addRecipeObject:)
@NSManaged public func addToRecipe(_ value: Recipe)

@objc(removeRecipeObject:)
@NSManaged public func removeFromRecipe(_ value: Recipe)

@objc(addRecipe:)
@NSManaged public func addToRecipe(_ values: NSSet)

@objc(removeRecipe:)
@NSManaged public func removeFromRecipe(_ values: NSSet)

}

1 个答案:

答案 0 :(得分:0)

您必须自己决定是否要管理CoreData类文件,或者是否希望Xcode为您管理它们。即使您告诉Xcode管理它们,您也创建了Recipe+CoreDataClass.swiftIngredient+CoreDataClass.swift。这也是你得到这个错误的原因:

 Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

此错误告诉您有多个具有相同名称的类。

因此,您要么删除RecipeIngredient扩展名,要让Xcode为您管理它们,或者您自己创建它们但是正确。如果您愿意,请按以下步骤操作:https://stackoverflow.com/a/40868074/5464989

手动创建这些文件后,您的应用似乎有效。