集成React Native中遇到的JSONKit错误

作者: c20cf494bb0f | 来源:发表于2016-12-01 20:02 被阅读391次

集成React Native完成之后,运行出现了如下错误:


错误截图

根据上下文,可以看出React Native调用了项目中JSONKit的方法,搜索全项目之后,发现并没有导入JSONKit,猜测是项目中某个框架有导入,再看报错,说的是野指针错误,可能跟这个解析方法有关系,所以,在不修改源码的情况下我的解决办法是:

//
//  NSObject+ExtensionMethod.h
//
//  Created by Pello on 2016/12/14.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSObject (ExtensionMethod)

+ (void)replaceJSONKitSelector;

@end
//
//  NSObject+ExtensionMethod.m
//
//  Created by Pello on 2016/12/14.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "NSObject+ExtensionMethod.h"

@implementation NSObject (ExtensionMethod)

+ (void)replaceJSONKitStringSelector
{
    SEL jsonStringSelectorOld = NSSelectorFromString(@"JSONStringWithOptions:error:");
    if ([NSDictionary instancesRespondToSelector:jsonStringSelectorOld]) {
        SEL jsonStringSelectorNew = NSSelectorFromString(@"NEWJSONStringWithOptions:error:");
        
        Method jsonStringMethodNew = class_getInstanceMethod([NSArray class], jsonStringSelectorNew);
        
        class_replaceMethod([NSArray class],
                            jsonStringSelectorOld,
                            method_getImplementation(jsonStringMethodNew),
                            method_getTypeEncoding(jsonStringMethodNew));
    }
}

+ (void)replaceJSONKitObjectSelector
{
    SEL jsonObjectSelectorOld = NSSelectorFromString(@"JSONStringWithOptions:error:");
    SEL jsonObjectMutableSelectorOld = NSSelectorFromString(@"JSONStringWithOptions:error:");
    if ([NSString instancesRespondToSelector:jsonObjectSelectorOld]) {
        
        SEL jsonObjectSelectorNew = NSSelectorFromString(@"NEWJSONStringWithOptions:error:");
        SEL jsonObjectMutableSelectorNew = NSSelectorFromString(@"NEWJSONStringWithOptions:error:");
        
        Method jsonObjectMethodNew = class_getInstanceMethod([NSArray class], jsonObjectSelectorNew);
        Method jsonObjectMutableMethodNew = class_getInstanceMethod([NSArray class], jsonObjectMutableSelectorNew);
        
        class_replaceMethod([NSString class],
                            jsonObjectSelectorOld,
                            method_getImplementation(jsonObjectMethodNew),
                            method_getTypeEncoding(jsonObjectMethodNew));
        
        class_replaceMethod([NSMutableString class],
                            jsonObjectMutableSelectorOld,
                            method_getImplementation(jsonObjectMutableMethodNew),
                            method_getTypeEncoding(jsonObjectMutableMethodNew));
    }
}

+ (void)replaceJSONKitSelector
{
    [self replaceJSONKitStringSelector];
    [self replaceJSONKitObjectSelector];
}

- (NSString *)NEWJSONStringWithOptions:(NSUInteger)option error:(NSError **)error
{
    return [self modelToJSONString];
}

- (id)NEWObjectFromJSONStringWithParseOptions:(NSUInteger)parseOptionFlags error:(NSError **)error
{
    return [self modelToJSONString];
}

- (id)NEWMutableObjectFromJSONStringWithParseOptions:(NSUInteger)parseOptionFlags error:(NSError **)error
{
    return [self modelToJSONString];
}

@end

新增一个扩展类,在初始化RCTRootView之前调用一次[NSObject replaceJSONKitSelector],问题就得到解决了。

大家在集成过程中是否有遇到同样的问题,或者有更好的解决方法,一起分享一下。。

相关文章

网友评论

  • aofeilin:怎么解决?
    c20cf494bb0f:升级一下Native使用到的第三方库试试
  • aofeilin:为什么我的不管用啊??
  • konmon:你好,我的也是做集成时遇到这个错误,情况跟你差不多,请问这两贴代码需要在哪里修改呢?
    c20cf494bb0f:@konmon JSONKit可能是某个第三方库引用的,ReactNative中并没有集成,在运行时检测项目中是否会响应JSONKit解析的方法,我再更新一下文档
    konmon:@Pello丶乐 RN版本已经是0.40了,JSONKit也没在项目中找到,从gitbub上下下来放到项目中,但问题依旧,奇怪的是只会在集成时才会抛的异常,想不明白。就在initWithBundle之前调用就OK了?
    c20cf494bb0f:在RCTRootView初始化之前走一次,有点简单粗暴,我这样做也没有办法的办法,建议尝试一下升级到最新版本,看看是否还有类似的问题。
  • louiszhuang86:您好。请问 [self yy_modelToJSONString]; 这个方法如何实现。
    c20cf494bb0f:这个是YYModel的方法,RN依赖JSONKit库,而JSONKit解析的时候crash,所以这里更换一下第三方库解析,建议不要像我这么干

本文标题:集成React Native中遇到的JSONKit错误

本文链接:https://www.haomeiwen.com/subject/lnffmttx.html