Ryan Copley
Ryan Copley

Reputation: 873

Obj-C enum redefinition error

typedef enum {
    artists = 0,
    artists_songs = 1,
    artist_albums = 2,
    albums = 3,
    album_songs = 4,
    tags = 5,
    tag = 6,
    tag_artists = 7,
    tag_albums = 8,
    tag_songs = 9,
    songs = 10,
    song = 11,
    playlists = 12,
    playlist = 13,
    playlist_songs = 14,
    search_songs = 15
} Methods;

typedef enum {
    artists = 0,
    albums = 1,
    songs = 2,
    tags = 3,
    playlists = 4    
} ReturnTypes;

I keep getting an error on the artists = 0 line for ReturnTypes, saying that artists has been re-declared. I'm not sure what the syntax error on this is. Any ideas?

Upvotes: 7

Views: 8706

Answers (3)

Richard
Richard

Reputation: 3376

An enum is just syntactic sugar for integer constants. You cannot define a given identifier in more than one place; in this case, you are trying to have the same names in multiple enums.
You could try something like classes with static members (rough illustration, not tested code):

@implementation MethodsEnum

+(int)artists
{
    return 0;
}

+(int)artists_songs
{
    return 1;
}

// etc.

@end

@implementation ReturnTypeEnum

+(int)artists
{
    return 0;
}

+(int)albums
{
    return 1;
}

// etc.

@end

Note that I am not recommending this approach, but it does emulate some of the language features you seem to be missing from Java's enum.

Upvotes: 0

Manlio
Manlio

Reputation: 10865

You have "artists" in both your enum types. The compiler doesn't care whether they have the same value or not, it throws an error.

Try redefining one of the two. You'll have the same problem for all other redefined constants.

Upvotes: 0

matt
matt

Reputation: 534885

The syntax error is that artists is being redeclared! You've declared it once in the first enum, now you're trying to declare it again in the second line. These enums are not separate types; they are just lists of constants. You can't have two constants called artists.

This is why enums in Cocoa have obnoxiously long boring names, such as UITableViewCellStyleDefault. It is so that they won't clash with one another. You should do the same, e.g. MyMethodsArtists vs. MyReturnTypesArtists.

Upvotes: 16

Related Questions