Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does passing an unsigned int to performSelector lose bits?

Tags:

objective-c

I am trying to pass a hex value as an unsigned int to a method using a dynamic link. The value I pass as a parameter is getting corrupted somehow. What is happening?

- (void)callPerformSelector
{
    NSNumber *argument = [NSNumber numberWithUnsignedInt:(unsigned int)0xFFFFFFFF];
    SEL selector = NSSelectorFromString(@"testPerformSelector:");
    NSLog(@"testPerformSelector object %@", argument);
   [self performSelector:selector withObject:argument];
}

- (void)testPerformSelector:(unsigned int) arg1
{
    NSLog(@"testPerformSelector unsigned int %u", arg1);
    NSLog(@"testPerformSelector hex %X", arg1);
}

Output is:

testPerformSelector object 4294967295
testPerformSelector unsigned int 4294967283
testPerformSelector hex FFFFFFF3
like image 377
Simon Fry Avatar asked Feb 06 '23 18:02

Simon Fry


1 Answers

Because it should be:

- (void)callPerformSelector
{
    NSNumber *argument = @0xFFFFFFFF;
    SEL selector = @selector(testPerformSelector:);
    NSLog(@"testPerformSelector object %@", argument);
   [self performSelector:selector withObject:argument];
}

- (void)testPerformSelector:(NSNumber *) arg1
{
    NSLog(@"testPerformSelector unsigned int %u", arg1.unsignedIntValue);
}

unsigned int and NSNumber * is two different things

like image 181
Cy-4AH Avatar answered Feb 13 '23 05:02

Cy-4AH