readFromURL的Swift NSDocument子类:ofType:错误:不被接受?

时间:2017-06-30 17:24:59

标签: swift nsdocument

我不理解NSDocument的要求;我只需要记录网址,以便在以后恢复Windows时,将再次检索网址。但是我使用Swift:我添加了" @ objc"常规思考是不同的。我来自我的客观传统,看起来堆栈名称使用了这种传统。最新的Swift版本(xcode 8.3)与7.3.1版本更加不同!

我尝试做的是将文档与网址关联,并在被要求将其转储到磁盘时。一旦我解决了这个问题,该文档将成为一个plist。从我原来的问题我可以放弃的第一个功能?

class PlayItem : NSObject {
    var name : String = k.item
    var link : URL = URL.init(string: "http://")!
    var time : TimeInterval
    var rank : Int

    override init() {
        name = k.item
        link = URL.init(string: "http://")!
        time = 0.0
        rank = 0
        super.init()
    }
    init(name:String, link:URL, time:TimeInterval, rank:Int) {
        self.name = name
        self.link = link
        self.time = time
        self.rank = rank
        super.init()
    }
    override var description : String {
        return String(format: "%@: %p '%@'", self.className, self, name)
    }
}

class Document : NSDocument {

    @objc func readFromURL(url: NSURL, ofType typeName: String, error outError: NSErrorPointer) -> Bool {
        self.fileURL = url as URL
        self.fileType = typeName
        return true
    }

    convenience init(contentsOf: URL, ofType: String) throws {
        self.init()

        switch ofType {
            case "internal":
            self.fileURL = contentsOf
            self.fileType = ofType
            break

        default:
            Swift.print("nyi")
        }
        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
   }

    override func read(from url: URL, ofType typeName: String) throws {
        switch typeName {
        case "internal":
            self.fileURL = url
            self.fileType = typeName
            break

        case "h2o":
            let data = try Data.init(contentsOf: url)
            do {
                try! self.read(from: data, ofType: typeName)
                self.fileURL = url as URL
                self.fileType = typeName
            }
            break

        default:
            Swift.print("nyi \(typeName)")
            break
    }
    throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)

    override func write(to url: URL, ofType typeName: String) throws {
        switch typeName {
        case "DocumentType":
            var temp = [Dictionary<String,AnyObject>]()
            for key in playlists.keys {
                var list = Array<AnyObject>()
                for playitem in playlists[key] as! [PlayItem] {
                    let item : [String:AnyObject] = [k.name:playitem.name as AnyObject, k.link:playitem.link.absoluteString as AnyObject, k.time:playitem.time as AnyObject, k.rank:playitem.rank as AnyObject]
                    list.append(item as AnyObject)
                }
                temp.append([k.name:key as AnyObject, k.list:list as AnyObject])
            }
            UserDefaults.standard.set(temp, forKey: UserSettings.Playlists.keyPath)
            UserDefaults.standard.synchronize()
            break

        case "h2o":
            let dict = NSDictionary.init()
            dict.setValue(playlists, forKey: UserSettings.Playlists.keyPath)
            dict.setValue(settings, forKey: UserSettings.PlayPrefs.keyPath)
            dict.write(to: url, atomically: true)
            break

        default:
            Swift.print("nyi \(typeName)")
            throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
        }
    }
}

出现此错误:

0   CoreFoundation                      0x00007fff89ed52cb __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00007fff9ece048d objc_exception_throw + 48
2   CoreFoundation                      0x00007fff89f53c3d +[NSException raise:format:] + 205
3   AppKit                              0x00007fff87e65a45 -[NSDocument readFromURL:ofType:error:] + 419
4   AppKit                              0x00007fff87b66dd7 -[NSDocument _initWithContentsOfURL:ofType:error:] + 172
5   AppKit                              0x00007fff87b66cbc -[NSDocument initWithContentsOfURL:ofType:error:] + 231

我怀疑投掷是否活跃,但我在每个功能上的突破都没有被击中?我想我的最近菜单永远不会正常,直到这个问题得到解决,并且每次运行都创建了比最后一个更多的窗口,所以我希望解决方案能够解决这些问题。

0 个答案:

没有答案