0

I have spent many hours trying to find a solution, so if it IS here somewhere and I missed it I am sorry ...

In .h

    @property (nonatomic, strong) NSArray *physicalMan;
    -(int) getManeuverRating:(int *) value;

In .m

    physicalMan = [[NSArray alloc] initWithObjects:@"Grip-Hold", @"Strike", @"Fall", @"Response", nil];

    NSLog(@" The second element is: %@", [physicalMan objectAtIndex:1]);
    NSLog (@" This is the index location of Grip-Hold: %i", [physicalMan indexOfObject:@"Grip-Hold"]);

    [self getManeuverRating:[physicalMan indexOfObject:@"Grip-Hold"]];
}

-(int) getManeuverRating:(int *) value
{
    int a = *value;
    return  a + 1;
}

The NSLogs work fine with the proper values, which is why I am so confused as to why the function will not work. The compiler warning says "Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'int *'" I have tried removing the * and I have tried to find other data types, and converting data types, and I cannot get anything to work correctly. Please help or point me in the right direction ... what am I doing wrong? what am I missing?

  • Why are you passing "value" by address?? indexOfObject doesn't return an address. (Wasn't the compiler screaming bloody murder about this?) – Hot Licks Aug 29 '13 at 01:04
  • possible duplicate of [Making pointer from integer in Objective C](http://stackoverflow.com/questions/8628279/making-pointer-from-integer-in-objective-c) – Chuck Aug 29 '13 at 01:05
  • I originally had int a = value; xcode told me to add the *, which was wrong, I needed to change (int *) into (NSUInteger) then go back to take * off value, and everything is good and happy. – Hammerhead96 Aug 29 '13 at 04:11

2 Answers2

1

The indexOfObject: method of NSArray returns an NSUInteger, not an int*. Passing an int to a method that takes int* is incorrect: the value at the corresponding memory location would not be valid.

You should change the getManeuverRating: method as follows:

-(int) getManeuverRating:(NSUInteger) value
{
    return  value + 1;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Indeed, build and run worked ... Thank you all for your time and input. – Hammerhead96 Aug 29 '13 at 00:54
  • 1
    I have learned that even if xcode says something is incompatible, still run it to see what happens, if I had, I would not have needed to post this question ... Now how do I close this thread? – Hammerhead96 Aug 29 '13 at 01:01
  • side note, it was NSUInteger, not NSUinteger ... capital i needed, but this was, indeed, the best fix, thanks – Hammerhead96 Aug 29 '13 at 04:07
1

You are not pointing to an int... you should make this function

-(NSInteger)getManeuverRating:(NSInteger) value
 {
    NSinteger a = value;
    return a + 1;
 }

If that is giving you issues you should also try casting the integer in the initial function...

So instead of

 [self getManeuverRating:[physicalMan indexOfObject:@"Grip-Hold"]];

Do

 NSInteger index = (NSInteger) [physicalMan indexOfObject:@"Grip-Hold"];
 [self getManeuverRating:index];

You should be using NSInteger instead of int simply because it is good to write in objective-c syntax. But it is just a wrapper. You could also make it take and return an NSUInteger and not cast it.

Another modernization thing you could do (and this is an aside) is declare your array like this...

 NSArray * physicalMan = @[@"Grip-Hold", @"Strike", @"Fall", @"Response"];
AdamG
  • 3,718
  • 2
  • 18
  • 28
  • I had trusted the compiler warning, I just now did a build + run ... ends up it works fine, just has those warning that SAY they are incompatible ... – Hammerhead96 Aug 29 '13 at 00:53
  • @Hammerhead96 - I'm sure the compiler was warning you about SOMETHING. But your "fix" was a major step in the wrong direction. – Hot Licks Aug 29 '13 at 01:06
  • I thought for some reason he wanted as an integer rather than unsigned int. Probably should have just assumed it was an unsigned int. – AdamG Aug 29 '13 at 01:17
  • I was trying to use an int because I had not realized that I needed an unsigned int ... Wrong "correction" was a side effect of xcode giving bad advice ... NSUInteger was the best fix. – Hammerhead96 Aug 29 '13 at 04:06