我是iPhone新手编程的新手。在我的项目中,我在头文件中有一个enum
:
enum SelectionType
{
BookSelection,
StartChapter,
EndChapter
}SType;
在我的项目中,我想知道我现在有哪enum
。为此,我尝试了以下方法,但它不起作用:
NSLog(@"stype is %c",SType);
我应该使用哪种格式说明符来获取NSLog
中的枚举?
答案 0 :(得分:2)
你必须自己做。 C没有这种反射能力。这是一个你可以使用的功能:
const char *STypeName(SType t)
{
switch (t) {
case BookSelection: return "BookSelection";
case StartChapter: return "StartChapter";
case EndChapter: return "EndChapter";
default: return NULL;
}
}
然后你可以调用函数SelectionTypeName
来获取名称:
SType stype = ...;
NSLog(@"stype = %s", STypeName(stype));
答案 1 :(得分: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);
答案 2 :(得分:0)
枚举基本上是int数据类型。您应该使用%d格式说明符。
答案 3 :(得分:0)
This method,从一个类似的问题,将让你解决这个问题。一系列宏,可帮助您在枚举值和字符串表示形式之间进行映射。