就是一bind就无法弹出应用窗口 按照书上 在attributed string 选择了 owner'file 但是运行显示不出来窗口
以下是document的程序 谢谢!
import Cocoa
enum NoteDocumentFileNames : String{
case TextFile = "Text.rtf"
case AttachmentsDirectory = "Attachments"
}
enum ErrorCode : Int {
case CannotAccessDocument
case CannotLoadFileWrappers
case CannotLoadText
case CannotAccessAttachments
case CannotSaveText
case CannotSaveAttachment
}
let ErrorDomain = "NotesErrorDomain"
func err(code: ErrorCode, _ userInfo:[NSObject:AnyObject]? = nil)
-> NSError{
return NSError(domain: ErrorDomain, code: code.rawValue, userInfo: userInfo as! [String : Any])
}
class Document: NSDocument {
var documentFileWrapper = FileWrapper(directoryWithFileWrappers: [:])
var text : NSAttributedString = NSAttributedString()
override init() {
super.init()
// Add your subclass-specific initialization here.
}
override class var autosavesInPlace: Bool {
return true
}
override var windowNibName: NSNib.Name? {
// Returns the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this property and override -makeWindowControllers instead.
return NSNib.Name("Document")
}
override func data(ofType typeName: String) throws -> Data {
// Insert code here to write your document to data of the specified type, throwing an error in case of failure.
// Alternatively, you could remove this method and override fileWrapper(ofType:), write(to:ofType:), or write(to:ofType:for:originalContentsURL:) instead.
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
override func read(from data: Data, ofType typeName: String) throws {
// Insert code here to read your document from the given data of the specified type, throwing an error in case of failure.
// Alternatively, you could remove this method and override read(from:ofType:) instead.
// If you do, you should also override isEntireFileLoaded to return false if the contents are lazily loaded.
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
override func fileWrapper(ofType typeName: String) throws -> FileWrapper {
let textRTFData = try self.text.data(
from: NSRange(0..<self.text.length),
documentAttributes: [
NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtf]
)
if let oldTextFileWrapper = self.documentFileWrapper
.fileWrappers?[NoteDocumentFileNames.TextFile.rawValue]{
self.documentFileWrapper.removeFileWrapper(oldTextFileWrapper)
}
self.documentFileWrapper.addRegularFile(withContents: textRTFData, preferredFilename: NoteDocumentFileNames.TextFile.rawValue)
return self.documentFileWrapper
}
override func read(from fileWrapper: FileWrapper, ofType typeName: String) throws {
guard let fileWrappers = fileWrapper.fileWrappers else{
throw err(code:.CannotLoadFileWrappers)
}
guard let documentTextData =
fileWrappers[NoteDocumentFileNames.TextFile.rawValue]?
.regularFileContents else {
throw err(code:.CannotLoadText)
}
guard let documentText = NSAttributedString(rtf: documentTextData,
documentAttributes: nil) else {
throw err(code:.CannotLoadText)
}
self.documentFileWrapper = fileWrapper
self.text = documentText
}
}