如何将带有字符串数组的结构转换为 NSData,反之亦然 Swift
How to Covert struct with an Array of string to NSData and vice versa Swift
我有这个结构:
struct MessageRandomWords {
let message = MessageType.kMessageTypeRandomWords
let randomWords : Array<Array<String>>
}
我正在尝试通过这样做将此结构转换为 NSDate:
var message = MessageRandomWords(randomWords: self.words)
let data = NSData(bytes: &message, length: sizeof(MessageRandomWords))
但是当我试图将其转换回原始结构时:
var messageRandomWords : MessageRandomWords?
data.getBytes(&messageRandomWords, length: sizeof(MessageRandomWords))
if let messageRandomWords = messageRandomWords {
}
我在 if let 语句中遇到 BAD_ACCESS
错误。
问题出在哪里?
基于这个答案:
我写了这个解决方案:
struct MessageRandomWords {
let message = MessageType.kMessageTypeRandomWords
var data : NSData?
var name: String
struct ArchivedPacket {
let message = MessageType.kMessageTypeRandomWords
var dataLength : Int64
var nameLength : Int64
}
func archive() -> NSData {
var archivedPack = ArchivedPacket(dataLength: Int64(self.data!.length), nameLength: Int64(self.name.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)))
var metaData = NSData(bytes: &archivedPack, length: sizeof(ArchivedPacket))
let archiveData = NSMutableData(data: metaData)
archiveData.appendData(name.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)
archiveData.appendData(data!)
return archiveData
}
static func unarchive(data : NSData!) -> MessageRandomWords {
var archivedPacket = ArchivedPacket(dataLength: 0, nameLength: 0)
let archivedStructLength = sizeof(ArchivedPacket) //lenght of the struct
//Get the data tha will form our archived Packet
let archivedData = data.subdataWithRange(NSMakeRange(0, archivedStructLength))
//save the data taht form the archivedPacket inside the archivedPacket
archivedData.getBytes(&archivedPacket, length: archivedStructLength)
//get the range of data that contains the name
let nameRange = NSMakeRange(archivedStructLength, Int(archivedPacket.nameLength))
//get the range of the data that contains the data
let dataRange = NSMakeRange(archivedStructLength + Int(archivedPacket.nameLength), Int(archivedPacket.dataLength))
//get the data that rappresent the name
let nameData = data.subdataWithRange(nameRange)
//Get the name frome the data
let name = NSString(data: nameData, encoding: NSUTF8StringEncoding) as! String
// Geth the data
let theData = data.subdataWithRange(dataRange)
//Create the struct
let messageRndm = MessageRandomWords(data: theData, name: name)
return messageRndm
}
}
如果您创建如图所示的结构,您可以通过将其编码为 NSData 来发送字符串数组,然后在收到时对其进行解码。
您可以在 GitHub
上找到完整的工作示例
如果您有更好的解决方案请留言反馈
我有这个结构:
struct MessageRandomWords {
let message = MessageType.kMessageTypeRandomWords
let randomWords : Array<Array<String>>
}
我正在尝试通过这样做将此结构转换为 NSDate:
var message = MessageRandomWords(randomWords: self.words)
let data = NSData(bytes: &message, length: sizeof(MessageRandomWords))
但是当我试图将其转换回原始结构时:
var messageRandomWords : MessageRandomWords?
data.getBytes(&messageRandomWords, length: sizeof(MessageRandomWords))
if let messageRandomWords = messageRandomWords {
}
我在 if let 语句中遇到 BAD_ACCESS
错误。
问题出在哪里?
基于这个答案:
struct MessageRandomWords {
let message = MessageType.kMessageTypeRandomWords
var data : NSData?
var name: String
struct ArchivedPacket {
let message = MessageType.kMessageTypeRandomWords
var dataLength : Int64
var nameLength : Int64
}
func archive() -> NSData {
var archivedPack = ArchivedPacket(dataLength: Int64(self.data!.length), nameLength: Int64(self.name.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)))
var metaData = NSData(bytes: &archivedPack, length: sizeof(ArchivedPacket))
let archiveData = NSMutableData(data: metaData)
archiveData.appendData(name.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!)
archiveData.appendData(data!)
return archiveData
}
static func unarchive(data : NSData!) -> MessageRandomWords {
var archivedPacket = ArchivedPacket(dataLength: 0, nameLength: 0)
let archivedStructLength = sizeof(ArchivedPacket) //lenght of the struct
//Get the data tha will form our archived Packet
let archivedData = data.subdataWithRange(NSMakeRange(0, archivedStructLength))
//save the data taht form the archivedPacket inside the archivedPacket
archivedData.getBytes(&archivedPacket, length: archivedStructLength)
//get the range of data that contains the name
let nameRange = NSMakeRange(archivedStructLength, Int(archivedPacket.nameLength))
//get the range of the data that contains the data
let dataRange = NSMakeRange(archivedStructLength + Int(archivedPacket.nameLength), Int(archivedPacket.dataLength))
//get the data that rappresent the name
let nameData = data.subdataWithRange(nameRange)
//Get the name frome the data
let name = NSString(data: nameData, encoding: NSUTF8StringEncoding) as! String
// Geth the data
let theData = data.subdataWithRange(dataRange)
//Create the struct
let messageRndm = MessageRandomWords(data: theData, name: name)
return messageRndm
}
}
如果您创建如图所示的结构,您可以通过将其编码为 NSData 来发送字符串数组,然后在收到时对其进行解码。
您可以在 GitHub
上找到完整的工作示例如果您有更好的解决方案请留言反馈