集成React Native完成之后,运行出现了如下错误:
![](https://img.haomeiwen.com/i3819087/1423d033b82bb68e.png)
根据上下文,可以看出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],问题就得到解决了。
大家在集成过程中是否有遇到同样的问题,或者有更好的解决方法,一起分享一下。。
网友评论
官方有解决方案。