NSData从NSKeyedArchiver到NSString [英] NSData from NSKeyedArchiver to NSString

查看:243
本文介绍了NSData从NSKeyedArchiver到NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将从NSKeyedArchiver生成的NSData转换为NSString,以便我可以传递它,并最终将其转换回NSData。我必须传递这作为一个字符串(我使用three20 URL传递)。我经历了各种编码,UTF8,ASCII等,并不能得到任何工作。 NSKeyedArchiver说NSData被形成为一个属性列表:NSPropertyListBinaryFormat_v1_0。

I'm trying to convert NSData generated from NSKeyedArchiver to an NSString so that I can pass it around and eventually convert it back to NSData. I have to pass this as a string (I'm using three20 URL passing). I've gone through various encodings, UTF8, ASCII, etc. and can't get anything to work. NSKeyedArchiver says that the NSData is formated as a property list: NSPropertyListBinaryFormat_v1_0.

有没有人有任何想法如何将这个NSData转换为字符串并返回?

Does anyone have any idea how I can convert this NSData to a String and back again? Size of the string isn't an issue.

感谢

推荐答案

您想要的是:

id<nscoding> obj;

NSData * data     = [NSKeyedArchiver archivedDataWithRootObject:obj];
NSString * string = [data base64EncodedString];

然后反过来

NSString * string;

NSData * data    = [NSData dataFromBase64String:string];
id<nscoding> obj = [NSKeyedUnarchiver unarchiveObjectWithData:data]



您可以添加base64EncodedString和dataFromBase64String: NSData + Base64 ,但默认情况下已包括

You can add base64EncodedString and dataFromBase64String: with the NSData category available here NSData+Base64 but it is now included by default

这篇关于NSData从NSKeyedArchiver到NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆