0

I am new to programming for the iPhone. In my project, I have an enum in a header file:

enum SelectionType
{

    BookSelection,
    StartChapter,
    EndChapter
}SType;

In my project, I want to know which enum I have right now. For that, I tried the following, but it doesn't work:

NSLog(@"stype is %c",SType);

Which format specifier should I use to get the enum in NSLog?

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • [This method](https://stackoverflow.com/a/202511/1320630), from a similar question, will let you solve this once. A series of macros that help you map between the enum value and it's string representation. – Bill Wilson Jun 29 '12 at 05:13

3 Answers3

2

You will have to do it yourself. C does not have this kind of reflection capability. Here is a function you could use:

const char *STypeName(SType t)
{
    switch (t) {
    case BookSelection: return "BookSelection";
    case StartChapter: return "StartChapter";
    case EndChapter: return "EndChapter";
    default: return NULL;
    }
}

And then you can call the function SelectionTypeName to get the name:

SType stype = ...;
NSLog(@"stype = %s", STypeName(stype));
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0
enum SelectionType {

BookSelection==0,//system provide the default value 0 for first enum then increase by one.
StartChapter==1,
EndChapter==2
}SType;
//then you check with 

if(sType==0)
  {
 //do something
}


else if(sType==1)
{

}

else
{

}
//you can use 
NSLog(@"Enum number=%i",sType);
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
0

enum is basically int data type. You should use %d format specifier.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42