-[NSInvocation getReturnValue:] with double value produces 0 unexpectedly

后端 未结 4 616
無奈伤痛
無奈伤痛 2021-02-05 12:22

I am trying to call a method that returns a double using NSInvocation. But I found that it does not working in 64 bit iOS apps. It works on on OS X, in

4条回答
  •  难免孤独
    2021-02-05 13:02

    You have no hope of getting this to work at the current time. I tried many variations of your code, and tested on an iPhone 5S and the latest Simulator in both 32 and 64 bit mode, but this must be a bug with arm64 since the 64bit simulator works just fine.

    So first I modified your code to try all kinds of variations, and it turns out using floatValue fails as well. Since the size of a float is common, this reduces the number of variables between the various trial platforms.

    Also, I tried to use a NSNumber target created using an integral and float method: the float method actually results in a crash! I tried other options like retaining the string and setting the invocation retain setting, no change.

    I've entered a bug on this: 15441447: NSInvocation fails only on arm64 devices - anyone concerned about it can dup it. I uploaded a demo project as well.

    The code I uploaded is:

    - (void)test
    {
        NSMethodSignature *signature = [NSString instanceMethodSignatureForSelector:@selector(floatValue)];
        for (int i = 0; i < 10; i++) {
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    
    #if 0
            NSString *str = [NSString stringWithFormat:@"%d", i];
            [invocation setTarget:str]; // fails on iPhone 5s
    #else
            //[invocation setTarget:[NSNumber numberWithFloat:(float)i]]; // crashes in 'invoke' on iPhone 5s, works fine in simulator
            [invocation setTarget:[NSNumber numberWithInteger:i]]; // fails on iphone 5S
    #endif
            [invocation setSelector:@selector(floatValue)];
    
            [invocation invoke];
    
            float f;
            [invocation getReturnValue:&f];
    
            NSLog(@"%f", f);
        }
    }
    

提交回复
热议问题