错误 React Native IOS 异常 '-[__NSDictionaryM 长度]:无法识别的选择器发送到

Error React Native IOS Exception '-[__NSDictionaryM length]: unrecognised selector sent to

我是 IOS 开发的新手,我已经在 React Native 中为 IOS 和 Android 开发并构建了一个应用程序,并且该应用程序在 Android 中完全可用.我还在 iPhone 模拟器上安装了该应用程序进行测试,在成功构建后,当应用程序打开时它崩溃了,Xcode 显示了错误堆栈。

堆栈跟踪

2018-03-26 17:18:59.537548+0530 VwWoocommerceAppMain[57934:681649] -[__NSDictionaryM length]: unrecognized selector sent to instance 0x60000023abc0
2018-03-26 17:18:59.555 [fatal][tid:com.facebook.react.AsyncLocalStorageQueue] Exception '-[__NSDictionaryM length]: unrecognized selector sent to instance 0x60000023abc0' was thrown while invoking multiSet on target AsyncLocalStorage with params (
        (
                (
            cart,
                        {
            }
        )
    ),
    5873
)
callstack: (
 0   CoreFoundation                      0x0000000103f8212b __exceptionPreprocess + 171
 1   libobjc.A.dylib                     0x0000000102874f41 objc_exception_throw + 48
 2   CoreFoundation                      0x0000000104003024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
 3   CoreFoundation                      0x0000000103f04f78 ___forwarding___ + 1432
 4   CoreFoundation                      0x0000000103f04958 _CF_forwarding_prep_0 + 120
 5   VwWoocommerceAppMain                0x0000000101956824 -[RCTAsyncLocalStorage _writeEntry:changedManifest:] + 258
 6   VwWoocommerceAppMain                0x0000000101957104 -[RCTAsyncLocalStorage multiSet:callback:] + 436
 7   CoreFoundation                      0x0000000103f0636c __invoking___ + 140
 8   CoreFoundation                      0x0000000103f06240 -[NSInvocation invoke] + 320
 9   CoreFoundation                      0x0000000103f1ec26 -[NSInvocation invokeWithTarget:] + 54
 10  VwWoocommerceAppMain                0x0000000101966929 -[RCTModuleMethod invokeWithBridge:module:arguments:] + 602
 11  VwWoocommerceAppMain                0x00000001019ac7a8 _ZN8facebook5reactL11invokeInnerEP9RCTBridgeP13RCTModuleDatajRKN5folly7dynamicE + 266
 12  VwWoocommerceAppMain                0x00000001019ac522 ___ZN8facebook5react15RCTNativeModule6invokeEjON5folly7dynamicEi_block_invoke + 78
 13  libdispatch.dylib                   0x0000000108bf32f7 _dispatch_call_block_and_release + 12
 14  libdispatch.dylib                   0x0000000108bf433d _dispatch_client_callout + 8
 15  libdispatch.dylib                   0x0000000108bfc855 _dispatch_queue_serial_drain + 1162
 16  libdispatch.dylib                   0x0000000108bfd1ea _dispatch_queue_invoke + 336
 17  libdispatch.dylib                   0x0000000108bf8f7c _dispatch_queue_override_invoke + 733
 18  libdispatch.dylib                   0x0000000108c00102 _dispatch_root_queue_drain + 772
 19  libdispatch.dylib                   0x0000000108bffda0 _dispatch_worker_thread3 + 132
 20  libsystem_pthread.dylib             0x00000001091465a2 _pthread_wqthread + 1299
 21  libsystem_pthread.dylib             0x000000010914607d start_wqthread + 13
)
2018-03-26 17:18:59.558030+0530 VwWoocommerceAppMain[57934:681649] Exception '-[__NSDictionaryM length]: unrecognized selector sent to instance 0x60000023abc0' was thrown while invoking multiSet on target AsyncLocalStorage with params (
        (
                (
            cart,
                        {
            }
        )
    ),
    5873
)
callstack: (
 0   CoreFoundation                      0x0000000103f8212b __exceptionPreprocess + 171
 1   libobjc.A.dylib                     0x0000000102874f41 objc_exception_throw + 48
 2   CoreFoundation                      0x0000000104003024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
 3   CoreFoundation                      0x0000000103f04f78 ___forwarding___ + 1432
 4   CoreFoundation                      0x0000000103f04958 _CF_forwarding_prep_0 + 120
 5   VwWoocommerceAppMain                0x0000000101956824 -[RCTAsyncLocalStorage _writeEntry:changedManifest:] + 258
 6   VwWoocommerceAppMain                0x0000000101957104 -[RCTAsyncLocalStorage multiSet:callback:] + 436
 7   CoreFoundation                      0x0000000103f0636c __invoking___ + 140
 8   CoreFoundation                      0x0000000103f06240 -[NSInvocation invoke] + 320
 9   CoreFoundation                      0x0000000103f1ec26 -[NSInvocation invokeWithTarget:] + 54
 10  VwWoocommerceAppMain                0x0000000101966929 -[RCTModuleMethod invokeWithBridge:module:arguments:] + 602
 11  VwWoocommerceAppMain                0x00000001019ac7a8 _ZN8facebook5reactL11invokeInnerEP9RCTBridgeP13RCTModuleDatajRKN5folly7dynamicE + 266
 12  VwWoocommerceAppMain                0x00000001019ac522 ___ZN8facebook5react15RCTNativeModule6invokeEjON5folly7dynamicEi_block_invoke + 78
 13  libdispatch.dylib                   0x0000000108bf32f7 _dispatch_call_block_and_release + 12
 14  libdispatch.dylib                   0x0000000108bf433d _dispatch_client_callout + 8
 15  libdispatch.dylib                   0x0000000108bfc855 _dispatch_queue_serial_drain + 1162
 16  libdispatch.dylib                   0x0000000108bfd1ea _dispatch_queue_invoke + 336
 17  libdispatch.dylib                   0x0000000108bf8f7c _dispatch_queue_override_invoke + 733
 18  libdispatch.dylib                   0x0000000108c00102 _dispatch_root_queue_drain + 772
 19  libdispatch.dylib                   0x0000000108bffda0 _dispatch_worker_thread3 + 132
 20  libsystem_pthread.dylib             0x00000001091465a2 _pthread_wqthread + 1299
 21  libsystem_pthread.dylib             0x000000010914607d start_wqthread + 13
)
2018-03-26 17:18:59.561965+0530 VwWoocommerceAppMain[57934:681649] *** Terminating app due to uncaught exception 'RCTFatalException: Exception '-[__NSDictionaryM length]: unrecognized selector sent to instance 0x60000023abc0' was thrown while invoking multiSet on target AsyncLocalStorage with params (
        (
                (
            cart,
                        {
            }
        )
    ),
    5873
)
callstack: (
 0   CoreFoundation                      0x0000000103f8212b __exceptionPreprocess + 171
 1   libobjc.A.dylib                     0x0000000102874f41 objc_exception_throw + 48
 2   CoreFoundation                      0x0000000104003024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
 3   CoreFoundation                      0x0000000103f04f78 ___forwarding___ + 1432
 4   CoreFoundation                      0x0000000103f04958 _CF_forwarding_prep_0 + 120
 5   VwWoocommerceAppMain                0x0000000101956824 -[RCTAsyncLocalStorage _writeEntry:changedManifest:] + 258
 6   VwWoocommerceAppMain                0x0000000101957104 -[RCTAsyncLocalStorage multiSet:callback:] + 436
 7   CoreFoundation                      0x0000000103f0636c __invoking___ + 140
 8   CoreFoundation                      0x0000000103f06240 -[NSInvocation invoke] + 320
 9   CoreFoundation                      0x0000000103f1ec26 -[NSInvocation invokeWithTarget:] + 54
 10  VwWoocommerceAppMain                0x0000000101966929 -[RCTModuleMethod invokeWithBridge:module:arguments:] + 602
 11  VwWoocommerceAppMain                0x00000001019ac7a8 _ZN8facebook5reactL11invokeInnerEP9RCTBridgeP13RCTModuleDatajRKN5folly7dynamicE + 266
 12  VwWoocommerceAppMain                0x00000001019ac522 ___ZN8facebook5react15RCTNativeModule6invokeEjON5folly7dynamicEi_block_invoke + 78
 13  libdispatch.dylib                   0x0000000108bf32f7 _dispatch_call_block_and_release + 12
 14  libdispatch.dylib                   0x0000000108bf433d _dispatch_client_callout + 8
 15  libdispatch.dylib                   0x0000000108bfc855 _dispatch_queue_serial_drain + 1162
 16  libdispatch.dylib                   0x0000000108bfd1ea _dispatch_queue_invoke + 336
 17  libdispatch.dylib                   0x0000000108bf8f7c _dispatch_queue_override_invoke + 733
 18  libdispatch.dylib                   0x0000000108c00102 _dispatch_root_queue_drain + 772
 19  libdispatch.dylib                   0x0000000108bffda0 _dispatch_worker_thread3 + 132
 20  libsystem_pthread.dylib             0x00000001091465a2 _pthread_wqthread + 1299
 21  libsystem_pthread.dylib             0x000000010914607d start_wqthread + 13
)', reason: 'Exception '-[__NSDictionaryM length]: unrecognized selector sent to instanc...'
*** First throw call stack:
(
 0   CoreFoundation                      0x0000000103f8212b __exceptionPreprocess + 171
 1   libobjc.A.dylib                     0x0000000102874f41 objc_exception_throw + 48
 2   VwWoocommerceAppMain                0x0000000101953a22 RCTFormatError + 0
 3   VwWoocommerceAppMain                0x00000001019ac9bc _ZN8facebook5reactL11invokeInnerEP9RCTBridgeP13RCTModuleDatajRKN5folly7dynamicE + 798
 4   VwWoocommerceAppMain                0x00000001019ac522 ___ZN8facebook5react15RCTNativeModule6invokeEjON5folly7dynamicEi_block_invoke + 78
 5   libdispatch.dylib                   0x0000000108bf32f7 _dispatch_call_block_and_release + 12
 6   libdispatch.dylib                   0x0000000108bf433d _dispatch_client_callout + 8
 7   libdispatch.dylib                   0x0000000108bfc855 _dispatch_queue_serial_drain + 1162
 8   libdispatch.dylib                   0x0000000108bfd1ea _dispatch_queue_invoke + 336
 9   libdispatch.dylib                   0x0000000108bf8f7c _dispatch_queue_override_invoke + 733
 10  libdispatch.dylib                   0x0000000108c00102 _dispatch_root_queue_drain + 772
 11  libdispatch.dylib                   0x0000000108bffda0 _dispatch_worker_thread3 + 132
 12  libsystem_pthread.dylib             0x00000001091465a2 _pthread_wqthread + 1299
 13  libsystem_pthread.dylib             0x000000010914607d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有知道的请帮忙

我 运行 也遇到过同样的问题。问题是 iOS 正在可变字典中寻找名为 length() 的函数,但 NSDictionaryM 使用 count()。解决方法是将您的对象字符串化,这样它就不会使用可变字典。

Android 代码工作正常,对象已存储并且没有指向错误函数的函数指针。

在我的项目中,我尝试使用异步存储来保存会话密钥。我有一个叫做存储的包装器。我也是新手,所以我可能不会在这里做最佳实践。

import {storage, keys} from './storage';

storage.store(keys.session, res.data).then(()=>{}).catch(()=>{}); //Will crash in iOS, but work fine in Android
storage.store(keys.session, JSON.stringify(res.data)).then(()=>{}).catch(()=>{}); //Will work fine on both.

完整方法如下:

getSession(email, password){
    Network.login(email, password)
    .then((res) => {
        if(res.code == 1){
            storage.store(keys.session, JSON.stringify(res.data)).then(()=>{
                //Hurray
            }).catch((error)=>{
                throw new Error(error);
            });
            store.dispatch({
                type: actions.SESSION, 
                session: res.data
            });
            return res.data;
        }else{
            throw new Error(res.data);
        }
    })
    .catch((err) => {
        this.setState({error: err.message});
    });
}

请避免在 ios 中的异步存储中使用空对象,这将解决您的问题

AsyncStorage 中保存数据时始终使用 JSON.stringify,无论您的数据是什么:

AsyncStorage.setItem('@key', 'will work on Android'))
AsyncStorage.setItem('@key', JSON.stringify('will work on both iOS and Android'))

对于IOS,您需要在初始化时输入空对象。

可以用下面的代码解决:

AsyncStorage.setItem('@key', JSON.stringify({}))

我的问题是 MapboxGL tileUrlTemplates 是一个内部包含 false 的数组。 在 Android 上 运行 时,错误消息要好得多,只是一个提示。