Defining Accessor Methods for BNRItem


#1

I’m getting errors on the following lines of code in BNRItem.m

[b]- (void)setValueInDollars:(int)i
{
valueInDollars = i; [color=#FF4000](Error: Incompatible integer to pointer conversion assigning to ‘int *’; take the address with &.)[/color]
}

  • (int)valueInDollars
    {
    return valueInDollars; [color=#FF4000](Error: Incompatible pointer to integer conversion returning ‘int *’ from a function with result type ‘int’; dereference with *)[/color]
    }[/b]

If I apply the recommend fixes the code looks like this:

[b]- (void)setValueInDollars:(int)i
{
valueInDollars = &i;
}

  • (int)valueInDollars
    {
    return *(valueInDollars);
    }[/b]

however my NSLog output looks like this:
2012-05-06 11:27:42.235 RandomPossessions[95294:403] Red Sofa (A1B2C): Worth $1799288668, recorded on 2012-05-06 16:27:42 +0000

i.e. the Worth should be $100 but changes to $1799288668. The NSLog output is fine with the code “not fixed”.


#2

In your BNRItem.h file, you seem to have:

when you should have:

and

when you should have:


#3

Close!

Most likely, the instance variable is declared like so:

int *valueInDollars;

Make sure that this is not a pointer:

int valueInDollars;


#4

[quote=“JoeConway”]Close!

Most likely, the instance variable is declared like so:

int *valueInDollars;

Make sure that this is not a pointer:

int valueInDollars;[/quote]

Ah, because you are returning an integer pointer from a function which should return int, like “-(int)function”. It’s right there… :stuck_out_tongue: