【问题标题】:SBJson iOS parser methods are Deprecated?SBJson iOS 解析器方法已弃用?
【发布时间】:2012-12-18 01:43:33
【问题描述】:

我正在使用 SBJson 解析器开始一个新项目,人们似乎在互联网上推荐它作为新 iOS 项目的最佳选择。我遇到了一个非常严重的问题,即 Stig Brautaset 声称您可以在 current release (3.1) 上使用的当前方法似乎已被弃用,或者至少我的编译器是这么说的。我似乎无法工作:

NSDictionary *dict = [responseString JSONValue];

这似乎是最新的方法,或者:

NSDictionary *dict = [parser objectWithString:responseString error:&error];

其中 parser 是一个 sbjson 解析器。 XCode 突出显示了这两个函数并告诉我它们已被弃用。

我在这里做错了什么??

【问题讨论】:

  • 为什么不用苹果的 NSJSONSerialization 类?
  • JSONKit 是最好的解析器。如果您不想使用外部代码和目标设备为 iOS 5.0 及更高版本,则可以使用 NSJSONSerialization。
  • 如果你不关心 iOS4.x 及以下版本(以及谁关心新项目?)我推荐 NSJSONSerialization,除非你特别需要流支持。不过目前SBJson的版本其实是3.2,你提到的方法的deprecation是well documented

标签: ios json sbjson


【解决方案1】:

查看源代码herehere看起来都是

- (id)objectWithString:(NSString*)jsonText error:(NSError**)error

- (id)JSONValue;

自 3.2 版起已弃用,并将在 4.0 版中删除。 你确定你用的是3.1?

另一方面

- (id)objectWithString:(NSString *)repr;

- (id)objectWithData:(NSData*)data;

看起来可用且未弃用。

我建议你改用它们。

另一种方法是使用 Apple 提供的 NSJSONSerialization 类。

【讨论】:

  • 谢谢,我最终使用了 NSJSONSerialization。不确定与 SBJson 的交易是什么,但我肯定拥有最新版本。那里的文档有点粗略
  • 你有更新的版本,显然是 3.2。但是,它缺少文档,这就是您没有看到弃用警告的原因。
  • 缺少什么文档? (我问只是为了改进它。)参见sbjson.org/news/2013/sbjson-v3.2.html 和例如这里的警告:sbjson.org/api/3.2/Classes/SBJsonParser.html#//api/name/…:
  • Stig,是的,您确实记录了它,但不仅仅是描述不推荐使用的东西,最好描述可以在其位置使用的东西。只是一个想法。
【解决方案2】:

只是使用 xcode it self 轻松解决问题的一般方法。 每当不推荐使用任何代码行时,最好按住键盘上的“alt”键并移过引发问题的对象并单击。 Xcode 然后建议编写代码的更好方法。 希望这会有所帮助

【讨论】:

  • 不适用于 SBJson 弃用:没有建议。
【解决方案3】:

尝试替换:

NSDictionary *dict = [responseString JSONValue];

与:

NSDictionary *dict = [[SBJsonParser new] objectWithString:responseString];

或者更好:

SBJsonParser *parser = [SBJsonParser new];
id object = [parser objectWithString:responseString];
if (parser.error || ![object isKindOfClass:[NSDictionary self]])
    @throw parser.error ?: @"not a json dictionary";// or any error handling alternative
NSDictionary *dict = object;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2014-11-15
    • 2014-08-05
    • 1970-01-01
    相关资源
    最近更新 更多