- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Xcode 8 在您的 NSManagedObject
子类上提供了一个自动生成的 extension
,看起来像这样:
// Animal+CoreDataProperties.swift
import Foundation
import CoreData
extension Animal {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Animal> {
return NSFetchRequest<Animal>(entityName: "Animal");
}
// etc...
}
这很棒,因为我们不再需要硬编码字符串来创建 NSFetchRequest
对象。但是,Apple 尚未提供引用此方法的协议(protocol),也未提供引用 entityName
本身的协议(protocol)。
例如,如果我想创建这样的方法:
protocol ManagedObjectType: class {}
extension ManagedObjectType where Self: NSManagedObject {
static func findOrCreate(inContext context: NSManagedObjectContext, matchingPredicate predicate: NSPredicate) -> Self {
// etc...
}
}
...我无法利用任何 NSManagedObject
子类的 fetchRequest
方法(因为它只定义在子类),所以我不得不自己再次对这些字符串进行硬编码(即使 Xcode 已经这样做了)。
知道解决这个问题的方法吗?我在想这些事情:
protocol ManagedObjectType: class {
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}
extension Animal: ManagedObjectType {}
但这不起作用,因为协议(protocol)不支持 where
子句,所以我不能指定 Self
是一个 NSFetchRequestResult
,这意味着当我开始使用它时,像这样:
let request: NSFetchRequest<Self> = self.fetchRequest()
... Xcode 给我这个错误:
'fetchRequest' produces 'NSFetchRequest<NSFetchRequestResult>', not the expected contextual result type 'NSFetchRequest<Self>'
有人解决过这个问题吗?
最佳答案
您可以为此目的使用模板。所以你的代码看起来像这样:
static func findOrCreate<T: NSManagedObject>(inContext context: NSManagedObjectContext, matchingPredicate predicate: NSPredicate) -> T {
// etc...
}
关于ios - findOrCreate 在 Swift 3 中没有硬编码 entityName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40630912/
SENARIO 我正在开发一个项目,将 Swift 3 合并到 Objective-C 项目中。该项目正在使用 CoreData,在我的 Objective-C AppDelegate 中,我声明了“
我使用简单的值,但我从未使用过值。(实体名称)。所以我对此感到困惑。我认为值用于获取当前值,但我在这里不明白。 public Tax Tax { get
我们在野外遇到过这种崩溃,当然我们不能在本地进行 repo 。它似乎只发生在 ios 7 上。它是一个 NSInternalInconsistencyException 的事实告诉我们这是一个内部 N
我是 coreData 编程的新手,我按照教程一步一步进行,当我收到此错误并陷入困境时,不知道这意味着什么 entityName.state must have a defined type
我正在尝试将 xml 文档加载到 C# 中的对象 XPathDocument 中。我的 xml 文档包括这一行:trés dégagée + rade当解析器到达那里时,它给了我这个错误:“解析 En
当使用“instruments”跟踪未引用的分配时,我看到了数千个 CDSnapshot[entityName]_ 实例。 这是什么?我认为它与 CoreData 相关? 最佳答案 是的,这与核心数据
我已经开始学习 Hibernate 并且有一个基本问题。 org.hibernate.Session 中的save(String entityName, Object object) 和save(Ob
以下查询有效: @Query("select o from Employee o where o.tenant.id = ?#{context.tenantId}") @Query("select o
在 Microsoft 的 Dynamics CRM 2011 实践实验室之后,我尝试将自定义 View 添加到响应特定属性的“onchange”的表单。这是我添加自定义 View 的函数: func
我发现我的代码中存在"&",这就是显示错误的原因 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(dsExport.Tables[0].R
当我设计QueryExpression 时,我总是使用以下硬编码语法。 QueryExpression expression = new QueryExpression { EntityName
我知道这可能看起来重复,但我尝试了几个答案,但所有这些都不起作用。 我正在尝试使用核心数据来存储已完成的级别,但我在保存数据时遇到了一些麻烦。当一个级别完成时,将调用以下行。 let appD
Xcode 8 在您的 NSManagedObject 子类上提供了一个自动生成的 extension,看起来像这样: // Animal+CoreDataProperties.swift impo
我正在为我的 winforms 应用程序使用自定义控制代码来创建自定义彩色进度条。我可以构建它,它工作正常,但每次我尝试使用设计器时,它都会崩溃。 “解析 EntityName 时出错” 如果我从工具
我在 Swift 2.1 和 Xcode 7.2 中使用核心数据此代码给我一个无法找到实体名称的错误。这因为 [Company] 不起作用。我有一个具有此名称的实体。 let fetchReq
我从下面的代码块中得到以下异常。 解析 EntityName 时出错。 Line1,位置 844。 我试图将从表中检索到的数据集解析为数据集。 public DataSet BindMasterDat
我有一个函数,我将一个字符串作为参数传递给名为 filterXML 的参数,其中一个属性中包含“&”。 我知道 XML 不会识别它,它会给我一个错误。这是我的代码: public XmlDocume
我正在尝试对我的实体之一的属性使用延迟加载 属性映射是这样的: 但是,当我尝试保存此实体的实例(使用 Linq)时,它会抛出 DatabaseQueryException 并带有以下内部异常: NH
我首先使用实体框架 6 代码。我有三个这样的实体: public class Doctor { public string DoctorID { get; set; } publi
我升级了一个从 ef4 开始的旧项目,但现在我将它迁移到了 ef5。 这是旧代码: protected void SaveEntity(T entity) { using (DocsM
我是一名优秀的程序员,十分优秀!