Search code examples
objective-cpointersautomatic-ref-countingimplicit-conversionnsobject

Why NSObject returning int, implicit cast error? Some pointer issue w/ ARC?


I have a method which returns a Card* NSObject (to be clear, this an object of my own invention, which I correctly and successfully define in my Card.h and Card.m):

-(Card*)readCardFromArrayWithCustomObjFromUserDefaults:(NSIndexPath *)indexPath;
{
    NSData *existingData = [[NSUserDefaults standardUserDefaults] objectForKey:@"Deck"];
    // if no such data exists, return nil
    if (existingData == nil)
        return nil;
    NSArray *existingDeck = [NSKeyedUnarchiver unarchiveObjectWithData:existingData];
    return [existingDeck objectAtIndex:indexPath.row];
}

It seems clear to me that this should return a Card*, and yet, when I call it from another method, like this:

Card* selectedCard = readCardFromArrayWithCustomObjFromUserDefaults(indexPath);

I get the warning:

Implicit conversion of 'int' to 'Card*' is disallowed with Arc

I thought maybe the issue was that I didn't really cast it to a Card* before returning, but if I change my readCardFromArrayWithCustomObjFromUserDefaults method to explicitly cast as such, I still get the same warning:

Card* thisCard = [existingDeck objectAtIndex:indexPath.row];
return thisCard;

To be clear, I know that this does return a Card*, because when I put that same code in the function calling it (instead of separating it into its own readCardFromArrayWithCustomObjFromUserDefaults method) I get the Card*. I am trying to slim down my code by putting this oft-used code portion in its own method and am baffled as to why it does not work.

I have tried returning my entire NSArray* of cards instead, but that causes the same warning (just implicit conversion to NSArray* instead of to 'Card*'). Existing answers to this warning, such as this and this do not seem related to my situation (unless I am really missing something).

I have 2 ideas regarding what might be happening:

  1. Maybe the fact that this data is being accessed elsewhere in order to populate a UITableView is somehow messing up the pointer or something.

  2. Maybe ARC is nixing my Card before I can even use it for some baffling reason (this sounds likely because the fact that the compiler seems to think my obviously Card* returning method returns an int makes me think I'm getting a pointer and my code for some reason doesn't understand that the pointer points at a Card*).

What am I doing wrong?


Solution

  • I feel exceedingly foolish. Then again, Stack Overflow is a place which makes you happy to feel foolish. The problem was just that I have been coding in c and C# for too long, so I called the method incorrectly. I should have written:

    Card* thisCard = [self readCardFromArrayWithCustomObjFromUserDefaults:indexPath];
    

    Instead of:

    Card* selectedCard = readCardFromArrayWithCustomObjFromUserDefaults(indexPath);