检测到NSTextView的缺失约束

时间:2017-02-01 03:49:33

标签: swift cocoa nstextview

我创建了一个基于文档的应用程序,它应该将带有图像的NSAttributedString保存到包中。我运行了应用程序并将图像添加到文本视图并保存。当我打开文件时,会出现一个对话框,说明' x'无法打开"这被打印到控制台:

[Layout] Detected missing constraints for <NSTextField: 0x100b3f480>.  It cannot be placed because there are not enough constraints to fully 
define the size and origin. Add the missing constraints, or set 
translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, 
you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

Document.swift:

enum CookRecipesFileNames : String {

    case notes = "Notes.rtfd"
}

class Document: NSDocument {

    var documentFileWrapper = FileWrapper(directoryWithFileWrappers: [:])

    var popover : NSPopover?

    var notes : NSAttributedString = NSAttributedString()

    ...

    override class func autosavesInPlace() -> Bool {
        return true
    }

    override func fileWrapper(ofType typeName: String) throws -> FileWrapper {

        let notesRTFdata = try self.notes.data(from: NSRange(0..<self.notes.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType])

        if let oldTextFileWrapper = self.documentFileWrapper.fileWrappers?[CookRecipesFileNames.notes.rawValue] {
            self.documentFileWrapper.removeFileWrapper(oldTextFileWrapper)
        }

        self.documentFileWrapper.addRegularFile(withContents: notesRTFdata, preferredFilename: CookRecipesFileNames.notes.rawValue)

        return self.documentFileWrapper
    }

    override func read(from fileWrapper: FileWrapper, ofType typeName: String) throws {
        guard let documentNotesData = fileWrappers[CookRecipesFileNames.notes.rawValue]?.regularFileContents else {
            throw err(.cannotLoadNotes)
        }

        guard let documentNotes = NSAttributedString(rtfd: documentNotesData, documentAttributes: nil) else {
            throw err(.cannotLoadNotes)
        }

        self.documentFileWrapper = fileWrapper

        self.notes = documentNotes
    }

}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

警告&#39;检测到缺失的约束......&#39;是一个构建警告,告诉您已向视图添加了约束,但不足以确定视图的x和y坐标以及高度和宽度。如果您在IB中没有添加约束(这里的选项是将它们全部删除),Xcode将告诉应用程序使用IB中使用的确切位置和尺寸。如果向视图添加任何约束,则必须添加足以确定坐标和两个维度的约束。如果您想保留约束(这是第二个选项),请转到IB,并在左上角查找黄色或红色警告错误:See this screenshot

这将为您提供视图控制器中缺失和冲突限制的列表。