开发者

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 (tableCol开发者_如何学Pythonumn == 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.


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;
}


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜