8

I am converting my iOS app to 64-bit. I have the latest Xcode 5.1 (beta 4) installed.

When I compiled the app, I received over 100 warnings and most of them are pretty easy to fix. However, I have a warning on the following code:

+ (CommentResponseStatus)commentReponseStatusCodeWithStatusString:(NSString *)_status
{
    NSArray *commentStatusString = [NSArray arrayWithObjects:@"success", @"needConfirmation", @"stopped", nil];

    return [commentStatusString indexOfObject:_status];
}

Where CommentResponseStatus is declared as:

typedef enum {
    success,
    needConfirmation,
    stopped
} CommentResponseStatus;

I have a warning "Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'CommentResponseStatus'"

The warning is on the line return [commentStatusString indexOfObject:_status];

In NSArray we have - (NSUInteger)indexOfObject:(id)anObject;

I am confused about this warning and don't know for now how to fix it. Any quick help would be appreciated.

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65

1 Answers1

16

According to apple docs about 64-bit changes.

Enumerations Are Also Typed : In the LLVM compiler, enumerated types can define the size of the enumeration. This means that some enumerated types may also have a size that is larger than you expect. The solution, as in all the other cases, is to make no assumptions about a data type’s size. Instead, assign any enumerated values to a variable with the proper data type

To solve this, create enumeration with type as below syntax.

typedef NS_ENUM(NSUInteger, CommentResponseStatus) {
    success,
    needConfirmation,
    stopped
};

or

typedef enum CommentResponseStatus : NSUInteger {
    success,
    needConfirmation,
    stopped
} CommentResponseStatus;
Mani
  • 17,549
  • 13
  • 79
  • 100
  • I tried the 2nd solution and the warning disappeared. I'm not sure about the 1st one though. I did some more searching and found similar answers here: http://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c – Abdullah Umer Jan 29 '14 at 13:25
  • I personally recommend the first one because it only has one place to refactor a name change. The latter has two and I also dislike the suffix naming style versus the prefix of the latter. – Erik Mar 14 '14 at 19:50