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?
@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
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()
{
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))
}
}
override class func autosavesInPlace() -> Bool
{
return true
}
override func dataOfType(typeName: String) throws -> NSData
{
var outError: NSError! = NSError(domain: "Migrator", code: 0, userInfo: nil)
outError = NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
if let value = self.noteText.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
return value
}
print("dataOfType ran.")
throw outError
}
override func readFromData(data: NSData, ofType typeName: String) throws
{
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}