当前位置:  开发笔记 > 前端 > 正文

NSMutableDictionary和JSONRepresentation

如何解决《NSMutableDictionary和JSONRepresentation》经验,为你挑选了1个好方法。

我下载框架JSON,添加到我的项目,我认为它是正确添加因为我放

#import 

我没有任何警告.但是,当我想使用它时,问题就开始了:

NSMutableDictionary * attachement = [NSMutableDictionary dictionary];
NSString *requestJson = [attachement JSONRepresentation];

但我对JSONRepresentation有一个警告:

Instance method '-JSONRepresentation' not found (return type defaults to 'id')

我该如何解决?

谢谢



1> Stavash..:

如果您使用的是iOS 5.0及更高版本,请使用内置版本 NSJSONSerialization

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:attachement 
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
if (!jsonData) {
    //Deal with error
} else {
    NSString *requestJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有