如何快速对NSData进行结构编码和解码?

leizh00701

我有以下结构定义:

struct ThreadManager: Equatable {
  let fid: Int
  let date: NSDate
  let forumName: String
  let typeid: Int
  var page: Int
  var threadList: [Thread]
  var totalPageNumber: Int?
}

和线程是:

  struct Thread: Equatable {
    let author: Author
    let replyCount: Int
    let readCount: Int
    let title: String
    let tid: Int
    let isTopThread: Bool
    var attributedStringDictionary: [String: NSAttributedString]
    var postDescripiontTimeString: String
    var hasRead: Bool
}

如何将ThreadManager变量编码为NSData?我尝试使用以下功能,但不起作用。

func encode<T>(var value: T) -> NSData {
  return withUnsafePointer(&value) { p in
    NSData(bytes: p, length: sizeofValue(value))
  }
}

func decode<T>(data: NSData) -> T {
  let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T))
  data.getBytes(pointer, length: sizeof(T))

  return pointer.move()
}

我有ThreadManager项,我想将它们存储到sqlite中。所以我需要将它们转换为NSData。我有一个名为threadManager的变量,其threadList中的项目数约为70。我运行代码并设置一个断点,并在xcode控制台中输入encode(threadManager),它只有73字节。这是错误的。我如何编码和解码那些结构到NSData。

leizh00701

@GwendalRoué:您是对的,但是我必须根据每个结构构建另一个类。我使用了以下方法,这很丑陋,但是可行。你能帮我改善吗?

init(data: NSData) {
    let dictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! NSDictionary
    fid = (dictionary["fid"] as! NSNumber).integerValue
    date = dictionary["date"] as! NSDate
    forumName = dictionary["forumName"] as! String
    typeid = (dictionary["typeid"] as! NSNumber).integerValue
    page = (dictionary["page"] as! NSNumber).integerValue
    totalPageNumber = (dictionary["totalPageNumber"] as? NSNumber)?.integerValue
    let threadDataList = dictionary["threadDataList"] as! [NSData]
    threadList = threadDataList.map { Thread(data: $0) }
}
extension ThreadManager {
func encode() -> NSData {
    let dictionary = NSMutableDictionary()
    dictionary.setObject(NSNumber(integer: fid), forKey: "fid")
    dictionary.setObject(date, forKey: "date")
    dictionary.setObject(forumName, forKey: "forumName")
    dictionary.setObject(NSNumber(integer: typeid), forKey: "typeid")
    dictionary.setObject(NSNumber(integer: page), forKey: "page")
    if totalPageNumber != nil {
        dictionary.setObject(NSNumber(integer: totalPageNumber!), forKey: "totalPageNumber")
    }
    let threadDataList: [NSData] = threadList.map { $0.encode() }
    dictionary.setObject(threadDataList, forKey: "threadDataList")

    return NSKeyedArchiver.archivedDataWithRootObject(dictionary)
}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何编码和解码此类

如何制作一个快速的Codable结构,以对JSON中的MongoDB _id ObjectId()进行解码和编码

如何使用Opus编码和解码音频

如何获得NSCoder对Swift的结构数组进行编码/解码?

如何对64进行编码和解码以找出最后几个零仅仅是填充?

如何在C中使用zlib库对websocket permessage-deflate进行编码和解码?

如何对对象列表进行编码和解码,以便以后将它们存储在sharedpreferences中?

如何在Objective-C中对数字数组进行编码和解码?

Android-使用私钥对RSA进行编码和解码?

使用zxing进行QR码编码和解码

使用 MySQL 在 Python 中进行字符编码和解码

使用 NSKeyedArchiver 和 NSKeyedUnarchiver 进行往返编码和解码

如何在base64字符串和numpy数组之间进行编码和解码?

如何编码和解码 QueryString 中包含特殊字符的 Url?

如何在python中编码和解码路径?

如何加入编码器和解码器

如何在C#中编码和解码特殊字符

如何在 google protobuff 中编码和解码向量

编码和解码国际字符

使用编解码器,unicode_escape()进行Python编码和解码

动态表达式可在逻辑应用中进行编码和解码

是否有任何单独的Java API用于对字符串进行编码和解码

Windows cmd和pycharm在Python 2.7.5.1中进行编码和解码会得到不同的结果

使用Spring Security,Spring Boot和MongoDB进行密码编码和解码

指定属性和字段以使用Newtonsoft JSON进行编码和解码

使用Caché对象脚本进行Base64编码和解码

在dotnet核心中使用密码和盐对字符串进行编码和解码

Base 64编码和解码示例代码

编码和解码字符串