gpt4 book ai didi

swift - 获取单个 Transformable 属性的请求 (Swift)

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:49 24 4
gpt4 key购买 nike

我有一个名为 NoteEntity 的核心数据实体(类型)。它有一个名为 noteDocument 的托管变量,它属于自定义类型 NoteDocument(我的 NSDocument 子类)。我更改了它自动生成的 NoteEntity+Core Data Properties 类,所以它显示为

import Foundation
import CoreData

extension NoteEntity {

@NSManaged var noteDocument: NoteDocument? // changed
@NSManaged var belongsTo: NSSet?

}

所以 noteDocument 是 NoteDocument 类型而不是 NSObject。 NoteDocument 类确实实现了 NSCoding,如下所示:

required convenience init(coder theDecoder: NSCoder)
{
let retrievedURL = theDecoder.decodeObjectForKey("URLKey") as! NSURL
self.init(receivedURL: retrievedURL)
}

func encodeWithCoder(theCoder: NSCoder)
{
theCoder.encodeObject(fileURL, forKey: "URLKey")
}

我希望能够在托管上下文中找到具有给定 noteDocument 值的 noteEntity 实体。所以我运行这段代码(向它传递一个参数 theNote,该参数对应于我知道托管上下文中存在的 noteDocument):

    var request = NSFetchRequest(entityName: "NoteEntity")
let notePredicate = NSPredicate(format: "noteDocument == %@", theNote)
request.predicate = notePredicate

print("Text in the NoteEntity with the NoteDocument for "+theNote.filename+":")

do
{
let notesGathered = try context.executeFetchRequest(request) as? [NoteEntity]

for n in notesGathered!
{
print (n.noteDocument!.filename)
print (n.noteDocument!.noteText)
}
}
catch let error as NSError
{
print("Could not run fetch request. \(error), \(error.userInfo)")
}

但它没有返回任何条目。如果我注释掉谓词,我将获得数据库中的所有 NoteEntity 值,但是其中的谓词我什么也得不到。显然我在谓词中尝试进行的搜索有问题。我认为这是因为该值是一个 Transformable,但我不确定从那里去哪里。我知道您不能对 Transformable 数组的成员运行提取请求,但是否不可能对单个 Transformable 属性运行提取请求?如果不是,还有哪些替代方案?

编辑:NoteDocument 类包含的内容比 NSCoding 多得多。正如我所说,它是一个 NSDocument 子类。 NSCoding 使用 URL 作为它的键,因为它是 NoteDocument 类的“主键”——它是初始化类的东西。这是类(class)的其余部分,不包括上面的 NSCoding:

import Cocoa

class NoteDocument: NSDocument, NSCoding
{
var filename: String
var noteText: String
var attributes: NSDictionary?
var dateCreated: NSDate?
var dateString: String?

init (receivedURL: NSURL)
{
self.filename = ""
self.noteText = ""
super.init()

self.fileType = "net.daringfireball.markdown"
self.fileURL = receivedURL

// Try to get attributes, most importantly date-created.
let fileManager = NSFileManager.defaultManager()
do
{
attributes = try fileManager.attributesOfItemAtPath(fileURL!.path!)
}
catch let error as NSError
{
print("The error was: "+String(error))
}

if let dateCreated = attributes?.fileCreationDate()
{
// print("dateCreated is "+String(dateCreated!))

// Format the date-created to an appropriate string.
dateString = String(dateCreated)
}
else
{
print("Did not find the attributes for "+filename)
}

if let name = self.fileURL?.lastPathComponent
{
filename = name
}
else
{
filename = "Unnamed File"
}

noteText = ""

do
{
noteText = try NSString(contentsOfURL: self.fileURL!, encoding: NSUTF8StringEncoding) as String
}
catch let error as NSError
{
print("Error trying to get note file:"+String(error))
}
}

// MARK: - Document functions

override class func autosavesInPlace() -> Bool
{
// print ("autosavesInPlace ran.")
return true
}

override func dataOfType(typeName: String) throws -> NSData
{
var outError: NSError! = NSError(domain: "Migrator", code: 0, userInfo: nil)

// Post: Document is saved to a file specified by the user.

outError = NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)

if let value = self.noteText.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
// Convert noteText to an NSData object and return that.
return value
}
print("dataOfType ran.")

throw outError

}

override func readFromData(data: NSData, ofType typeName: String) throws
{
// Currently unused; came free with NSDocument.
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}


}

最佳答案

在代码中,您表明您正在编码的唯一内容是 URL。在这种情况下,在实体中使用纯字符串来存储 URL 并为文档添加 transient 属性(或创建包装类以组合实体和文档)更有意义并提供更多实用程序。通过这种方式,您可以在谓词中使用 URL,并且很容易从文档构建谓词。将文档存储为可转换文件并不能以任何方式帮助您。

关于swift - 获取单个 Transformable 属性的请求 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556553/

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