TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Warning "Return makes pointer from integer without a cast"

Why does it have a "Return makes pointer from integer without a cast" warning?

I have a "Person" object that contains an NSString and an int.

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Person *thePerson = [theList objectAtIndex:row];
if (tableColumn == nameTableColumn)
    {
        return thePerson.theName;
    }
    else if (tableColumn == ageTableColumn)
    {
        return thePerson.theAge;
    }
    else
         return nil;
}

For some kind of reason, the Age instance variable gets a warning about pointers. It's weird and I want to know why.

Upvotes: 0

Views: 2378

Answers (2)

extremeboredom
extremeboredom

Reputation: 1502

id is the type for a generic object, and is actually a pointer (even though you don't have to declare it with the '*'). int isn't an object, and so you get the warning. If you want to use this method, you could try wrapping it in an NSNumber:

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
Person *thePerson = [theList objectAtIndex:row];
if (tableColumn == nameTableColumn)
    {
        return thePerson.theName;
    }
    else if (tableColumn == ageTableColumn)
    {
        return [NSNumber numberWithInt:thePerson.theAge];
    }
    else
         return nil;
}

Upvotes: 1

BoltClock
BoltClock

Reputation: 723598

int is a value type, but id corresponds to an arbitrary Objective-C pointer type.

You can wrap the int in an NSNumber and return that:

return [NSNumber numberWithInt:thePerson.theAge];

Upvotes: 0

Related Questions