weixin_43239783 2018-10-16 14:26
浏览 336

在 learning swift书中开发Mac osx记事本应用时无法将document和text view bind起来

就是一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
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多