Objective-C:How to Converting int to id for selector’s object

objective c

I received a return value from SQLite fetch

int primaryKey = sqlite3_column_int(statement, 0);

and I'm going to use it as a selector's object:

[[ABC alloc] performSelector:@selector(abcWithAAA:) withObject:[NSNumber numberWithInt:primaryKey]];

the NSLog result for primarykey is a number 4:

NSLog(@"primaryKey:%i",primaryKey);
4

but the NSLog result for [NSNumber numberWithInt:primaryKey] is 131628896.

why? and how do i convert the int value correctly?

Thanks!

Best Answer

I solved the problem using an adapter method that does the cast for the withObject method. My problem was that I wanted to use a typedef enum and pass it as value to the withObject.

I wanted to call this method using the performSelect message:

-(void) requestInfosAndPersistByMonsterType:(MonsterTypes)monsterType {

}

As you see it request a MonsterTypes typedef defined like this:

typedef enum
{
    MonsterTypeIWerwolf = 0,
    MonsterTypeITempler = 1,
    MonsterTypeIUndefined,
} MonsterTypes;

Actually to be able to call the method above I build this adapter that calls it then:

   -(void)monsterTypeFromObject:(id)_monsterType {
        if ([_monsterType respondsToSelector:@selector(intValue)]) {
            int _t = [_monsterType intValue];
            switch (_t) {
                case MonsterTypeIWerwolf:
                    _t = MonsterTypeIWerwolf;
                    break;
                case MonsterTypeITempler:
                    _t = MonsterTypeITempler;
                    break;
                default:
                    _t = MonsterTypeIUndefined;
                    break;
            }
            [self requestInfosAndPersistByMonsterType:_t];
        }
    }

It is used this way:

[self performSelector:@selector(monsterTypeFromObject:) withObject:[NSNumber numberWithUnsignedInt:monsterType] afterDelay:5.0f];

You can find it explained in more detail here: http://kerkermeister.net/objective-c-adapter-from-nsinteger-to-id-when-using-performselector-withobject/