JSPatch icon indicating copy to clipboard operation
JSPatch copied to clipboard

Can't convert signed char type with OCNumber

Open ideaPods opened this issue 8 years ago • 4 comments

I want to convert "-1" with OCNumber to NSNumber, but after use: var oc_n = OCNumber("NSNumber", "numberWithChar:", [-1]);

then oc_n.charValue() return "255", how to resolve this issue, please?

ideaPods avatar Mar 14 '17 02:03 ideaPods

Test: XCode->Edit Scheme->Run->Build Configuration->Debug: oc_n.charValue() return -1; XCode->Edit Scheme->Run->Build Configuration->Release: oc_n.charValue() return 255;

Why?

ideaPods avatar Mar 14 '17 07:03 ideaPods

How should I get a signed return value after an jspatch call? I need -1 but not 255 on native.

ideaPods avatar Mar 14 '17 07:03 ideaPods

  • 过程:

用jspatch重写了接口,返回的代码是:

getSignedKey: function() { var oc_n = OCNumber("NSNumber", "numberWithChar:", [-1]); console.log("get value:"+ oc_n.charValue()); return oc_n.charValue(); }

console log是:

"get value:-1"

我的本地接口获取到的值却有不同:

char signKey = self.getSignedKey(); NSLog("the signed key: %d, %@", signKey, @(signKey)); ``

  • 测试结果:
  1. XCode->Edit Scheme->Run->Build Configuration->Debug:

获取到的值: "the signed key: -1, -1"

  1. XCode->Edit Scheme->Run->Build Configuration->Release:

获取到的值: "the signed key: 255, -1"

  • 问题:

    release模式,获取到的为啥是-1的补码255?

  • 要求:

    期望: getSignedKey()接口返回的值,总是-1, 而不是补码.

  • 影响: 而我的存储字典里的键值对key是用的signed char类型如: signed char aKey = -1; [dictionary setObject:@"value_1" forKey:@(aKey)]; ` 当我通过getSignedKey()接口,获取到 'signedKey'后,去查找字典, 前者能成功匹配, 而后者不能匹配到key;

  • 请问这个问题怎么解决?

ideaPods avatar Mar 14 '17 08:03 ideaPods

建议尝试:

  1. 看 release 和 debug 的build settings 有什么不同
  2. 测试直接在OC调 numberWithChar,再输出 charValue 看是不是结果一样

bang590 avatar Mar 31 '17 06:03 bang590