【问题标题】:JSON error on iPhone appiPhone应用程序上的JSON错误
【发布时间】:2011-03-02 19:38:06
【问题描述】:

当我尝试在我的模拟器上运行应用程序时出现错误。 我已经在我的应用程序中复制了 JSON(JSON 框架)项目,但我的模拟器在编译以下行时崩溃,(我使用的是 iOS 4.2)

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *jsonString = [NSString stringWithString:@"{\"foo\": \"bar\"}"];
    NSDictionary *dictionary = [jsonString JSONValue];
    NSLog(@"Dictionary value for \"foo\" is \"%@\"", [dictionary objectForKey:@"foo"]);
}

错误:

2011-03-02 13:33:37.304 test[10918:207] -[NSCFString JSONValue]: unrecognized selector sent to instance 0x3034
2011-03-02 13:33:37.308 test[10918:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString JSONValue]: unrecognized selector sent to instance 0x3034'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00da7be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00efc5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00da96fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d19366 ___forwarding___ + 966
    4   CoreFoundation                      0x00d18f22 _CF_forwarding_prep_0 + 50
    5   test                                0x000025d7 -[testViewController viewDidLoad] + 119
    6   UIKit                               0x0035e65e -[UIViewController view] + 179
    7   test                                0x0000237e -[testAppDelegate application:didFinishLaunchingWithOptions:] + 79
    8   UIKit                               0x002b11fa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    9   UIKit                               0x002b355e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    10  UIKit                               0x002bddb2 -[UIApplication handleEvent:withNewEvent:] + 1533
    11  UIKit                               0x002b6202 -[UIApplication sendEvent:] + 71
    12  UIKit                               0x002bb732 _UIApplicationHandleEvent + 7576
    13  GraphicsServices                    0x016dda36 PurpleEventCallback + 1550
    14  CoreFoundation                      0x00d89064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    15  CoreFoundation                      0x00ce96f7 __CFRunLoopDoSource1 + 215
    16  CoreFoundation                      0x00ce6983 __CFRunLoopRun + 979
    17  CoreFoundation                      0x00ce6240 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00ce6161 CFRunLoopRunInMode + 97
    19  UIKit                               0x002b2fa8 -[UIApplication _run] + 636
    20  UIKit                               0x002bf42e UIApplicationMain + 1160
    21  test                                0x0000230c main + 102
    22  test                                0x0000229d start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
(gdb) 

【问题讨论】:

  • 您使用的是哪个 JSON 框架?您是否在需要它的类中导入它?您正在尝试将JSONValue 发送到NSString,它没有这样的方法,尽管我认为这个框架可以利用类别来加粗这个功能。
  • 我实际上已经从互联网上下载了 JsoniPhoneSDK.dmg 文件。我已按照其中一个教程的建议在我的项目中添加了 JSON 文件夹。在我的 appController.m 文件中包含 JSON.h 文件。我使用下面的教程安装 JSON mobileorchard.com/tutorial-json-over-http-on-the-iphone
  • 在将源文件添加到项目时,您是否选中了复制源文件的复选框?如果没有,并且您删除了原始磁盘映像,这可能是您的问题。
  • 是的,我做到了..但是您认为这可能是因为我使用的是 XCode 3.2.5 还是与此无关。

标签: objective-c cocoa-touch ios4 json-framework


【解决方案1】:

尝试将-ObjC -all_load 添加到 OTHER_LINKER_FLAGS 设置。如果这不是一个选项,您可以尝试直接使用 SBJsonParser 或 SBJsonWriter 对象而不是类别方法。

【讨论】:

  • OTHER_LINKER_FLAGS 选项不起作用。我会尝试看看另一个是否有效..
  • @developer,您需要添加 both 设置。
【解决方案2】:

确保正在编译 SBJSON 文件! 目标->构建阶段->编译文件

【讨论】:

    【解决方案3】:

    将 -ObjC -all_load 添加到应用程序目标 -> 构建设置 -> 链接 -> 其他链接器标志 -> 调试在 XCode 4 中工作。

    之前它崩溃了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 2013-10-01
      • 2012-11-11
      • 2011-04-10
      • 1970-01-01
      • 2013-08-25
      • 2012-06-06
      相关资源
      最近更新 更多