带有Multipeer传输错误的swift 3.0中的NS编码

时间:2016-09-22 18:54:46

标签: ios swift nscoding multipeer-connectivity

所以我用它来编码一些数据......

func save2fileV3() -> String {
    var newDBstrip:[Book] = []
    for blah in fnDB {
        let newDBrec = Book(name: blah.fnName, link: blah.fnLink, age: blah.fnPos)
        newDBstrip.append(newDBrec)
    }
    let directory = NSTemporaryDirectory()
    let fileName = NSUUID().uuidString
    let fileURL = directory.appending(fileName)
   NSKeyedArchiver.archiveRootObject(newDBstrip, toFile: fileURL)
    guard let encodedBook = NSKeyedUnarchiver.unarchiveObject(withFile: fileURL) as? [Book] else { return "" }
   encodedBook.forEach({print( $0.fileName, $0.fileLink, $0.filePos)})
    return fileURL
}

它有效,bon;所以我通过MultipeerConnectivity链接发送它......

self.appDelegate.mpcHandler.session.sendResource(at: fileURL as URL, withName: fileSavedUnder, toPeer: self.appDelegate.mpcHandler.session.connectedPeers[0])

使用此功能在其他主机上接收...

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?) {
   print("File finished \(localURL)")

    do {
        let  localFS = try String(contentsOf: localURL)
        let encodedBook = NSKeyedUnarchiver.unarchiveObject(withFile: localFS) as? [Book]
        encodedBook.forEach({print( $0.fileName, $0.fileLink, $0.filePos)})
    } catch {
        print("conversion failed")
    }

}

但它无法解码......我在这里遗漏了一些东西,一个小细节...我知道发送文件链接有效;我已经发送了一个纯文本文件并收到了它。但我正在尝试使用这个NSCoding库以正确的方式编码...

这是NSCoding代码还没有完全放弃......

class Book: NSObject, NSCoding {
let fileName: String
let fileLink: String
let filePos: String
required init(name: String, link: String, age: String ) {
    self.fileName = name
    self.fileLink = link
    self.filePos = age
}
required init(coder decoder: NSCoder) {
    self.fileName = decoder.decodeObject(forKey: "fileName") as? String ?? ""
    self.fileLink = decoder.decodeObject(forKey: "fileLink") as? String ?? ""
    self.filePos = decoder.decodeObject(forKey: "filePos")  as? String ?? ""
}

func encode(with coder: NSCoder) {
    coder.encode(fileName, forKey: "fileName")
    coder.encode(fileLink, forKey: "fileLink")
    coder.encode(filePos, forKey: "filePos")
}
}

1 个答案:

答案 0 :(得分:1)

所以,我保存到文件中......

func save2file() -> String {
    let directory = NSTemporaryDirectory()
    let fileName = NSUUID().uuidString
    let fileURL = directory.appending(fileName)

    var bytesWritten = 0
    if let outputStream = OutputStream(toFileAtPath: fileURL, append: true) {
        outputStream.open()
        for blah in fnDB {
            let string2write = String(format: "%@,%@,%d\n", blah.fnLink , blah.fnName, blah.fnPos)
            print(string2write)
            outputStream.write(string2write,maxLength: string2write.lengthOfBytes(using: String.Encoding.utf8))
            bytesWritten = bytesWritten + string2write.lengthOfBytes(using: String.Encoding.utf8)
        }
        outputStream.close()
        }
    return fileURL
    }

转移它......然后用它读回来......

 do {
        let fullText = try String(contentsOf: localURL)
        let lines = fullText.components(separatedBy: "\n") as Array
        for blah in lines {
            let field = blah.components(separatedBy: ",") as Array
            if field.count == 3 {
                    sharedDataAccess.fnATV(fnName: field[1], fnLink: field[0], fnPos: Int(field[2])!)
            }
            sharedDataAccess.sorterPos()
            sharedDataAccess.dump()
        }
    } catch {
        print("fcuk2209 conversion failed")
    }

它比NSCoding痛苦得多......:)