Fetch request for a single Transformable attribute (Swift)

I have a Core Data object (type) called NoteEntity. It has a managed variable called noteDocument, which is of type NotesDocument (my subclass of NSDocument). I modified my automatically generated DataEntity + Core Data Properties class to read

import Foundation
import CoreData

extension NoteEntity {

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

}

so noteDocument is of type NoteDocument instead of NSObject. The NoteDocument class implements NSCoding as follows:

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")
}

What I want to do is find noteEntity objects in a managed context with the given NoteDocument value. Therefore, I run this code (passing it theNote parameter corresponding to the noteDocument character, which, as I know, exists in a controlled context):

    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? , ?

EDIT: NoteDocument , NSCoding. , NSDocument. NSCoding URL , " " NoteDocument - , . , 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)
}


}
+4
1

, , , URL. URL- ( - ) . , URL- . .

+1

Source: https://habr.com/ru/post/1648966/


All Articles