8

I am looping through an NSString object called previouslyDefinedNSString and verifying if the integer representing the ASCII value of a letter is in an NSMutableSet called mySetOfLettersASCIIValues, which I had previously populated with NSIntegers:

NSInteger ASCIIValueOfLetter;

    for (int i; i < [previouslyDefinedNSString length]; i++) {

        ASCIIValueOfLetter = [previouslyDefinedNSString characterAtIndex:i];

        // if character ASCII value is in set, perform some more actions...
        if ([mySetOfLettersASCIIValues member: ASCIIValueOfLetter])

However, I am getting this error within the condition of the IF statement.

Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'id';
Implicit conversion of 'NSInteger' (aka 'int') to 'id' is disallowed with ARC

What do these errors mean? How am I converting to an object type (which id represents, right?)? Isn't NSInteger an object?

2
  • member: presumably expects an id. You're giving it an integer.
    – Hot Licks
    Commented Apr 1, 2012 at 3:25
  • 1
    Okay, so the problem here is that a set must contain objective-c objects and not scalar values like NSInteger.
    – lnafziger
    Commented Apr 1, 2012 at 3:27

2 Answers 2

19

You want to make it an NSNumber, as in:

NSInteger ASCIIValueOfLetter;

    for (int i; i < [previouslyDefinedNSString length]; i++) {

        ASCIIValueOfLetter = [previouslyDefinedNSString characterAtIndex:i];

        // if character ASCII value is in set, perform some more actions...
        if ([mySetOfLettersASCIIValues member: [NSNumber numberWithInteger: ASCIIValueOfLetter]])

Now you're going to have the result you're looking for.

1
  • 1
    How bizarre... my solution was initWithStyle: [NSNumber numberWithInteger: UITableViewCellStyleDefault], instead of initWithStyle:UITableViewCellStyleDefault
    – Morkrom
    Commented May 15, 2013 at 15:32
11

These errors mean that member: expects an object. id is a pointer to an Objective-C object, and instead of an object, you're passing in a primitive type, or scalar (despite its NS- prefix, NSInteger is not an object - just a typedef to a primitive value, and in your case, an int). What you need to do is wrap that scalar value in an object, and specifically, NSNumber, which is a class specifically designed to handle this.

Instead of calling member: with ASCIIValueOfLetter, you need to call it with the wrapped value, [NSNumber numberWithInteger:ASCIIValueOfLetter], as Maurício mentioned.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.