gpt4 book ai didi

ios - swift 3.0 中的 NScoding 与 Multipeer 传输错误

转载 作者:行者123 更新时间:2023-11-28 06:33:29 26 4
gpt4 key购买 nike

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

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")
}
}

最佳答案

所以,我用这个保存到文件中......

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 痛苦......:)

关于ios - swift 3.0 中的 NScoding 与 Multipeer 传输错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39646867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com