Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning on typedef enum when converting app to 64-bit

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.

like image 674
Abdullah Umer Avatar asked Jan 29 '14 12:01

Abdullah Umer


People also ask

Can enum be 64 bit?

For C++ and relaxed C89/C99/C11, the compiler allows enumeration constants up to the largest integral type (64 bits).

How do you specify the size of an enum?

The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.

How do you declare an enum in Objective C?

Enums are defined by the following the syntax above. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA, MyEnumValueB, MyEnumValueC, }; You also can set your own raw-values to the enumeration types. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, };

What is Ns_enum?

Enumeration Macros }; The NS_ENUM macro helps define both the name and type of the enumeration, in this case named UITableViewCellStyle of type NSInteger . The type for enumerations should be NSInteger .


1 Answers

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;
like image 81
Mani Avatar answered Oct 21 '22 10:10

Mani