Enj Obj-C override 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 in the line artist = 0 for ReturnTypes, saying that the artists were re-declared. I am not sure what the syntax error is. Any ideas?

+4
source share
3 answers

The syntax error is that artists updated! You declared it once in the first listing, now you are trying to declare it again in the second line. These enumerations are not separate types; these are just lists of constants. You cannot have two constants called artists .

This is why Cocoa listings have shockingly long, boring names like UITableViewCellStyleDefault . This is so that they will not collide with each other. You have to do the same, for example. MyMethodsArtists vs MyReturnTypesArtists .

+13
source

You have β€œartists” in both types of listings. The compiler does not care if they have the same value or not, it throws an error.

Try overriding one of the two. You will have the same problem for all other overridden constants.

0
source

An enum is just syntactic sugar for integer constants. You cannot define this identifier in several places; in this case, you are trying to have the same name in multiple listings.
You can try something like classes with static members (rough illustration, untested 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 

Please note that I do not recommend this approach, but it emulates some of the language features that you think are missing from the Java enum .

0
source

All Articles