2011-12-08 55 views
0

我想用类似的枚举类型定义:选择由IF-THEN-ELSE条件语句

if (foo>0){ 
typedef enum { 
    Form_FirstName = 0, 
    Form_NamePrefix, 
    Form_LastName, 
    Form_Email, 
    Form_Phone 
} Form; 
} else { 
    typedef enum { 
    Form_FirstName = 0, 
    Form_LastName, 
    Form_Phone 
} Form; 
} 

可以这样做?我应该在哪里做这件事?在.m中还是在.h中?我想用这个UITableView。

+0

编译器说什么? – onnoweb

回答

3

首先,枚举是编译类型结构。

其次,你有一个范围界定问题。因为您正在定义ifelse范围内的枚举类型。它不会在if..else..声明之外具有可视性。

您需要找到一种不同的方式来区分基于状态的索引。

更新基于OP的后续的问题:

OK,你需要地图的某种。例如,你可以这样做:

定义你的枚举。

enum { 
    Form_FirstName = 0, 
    Form_NamePrefix, 
    Form_LastName, 
    Form_Email, 
    Form_Phone 
}; 

假设你的类有一个indexes伊娃与通常@property@synthesize,请设置您的索引:

if (foo>0) { 
    self.indexes = [NSArray arrayWithObjects:[NSNumber numberWithInt:Form_FirstName],[NSNumber numberWithInt:Form_NamePrefix],[NSNumber numberWithInt:Form_LastName],[NSNumber numberWithInt:Form_Email],[NSNumber numberWithInt:Form_Phone],nil]; 
} 
else { 
    self.indexes = [NSArray arrayWithObjects:[NSNumber numberWithInt:Form_FirstName],[NSNumber numberWithInt:Form_LastName],[NSNumber numberWithInt:Form_Email],[NSNumber numberWithInt:Form_Phone],nil]; 
} 

在其他地方,当你需要一个枚举领域转化为一个索引:

NSInteger index = [self.indexes indexOfObject:[NSNumber numberWithInt:<Form Enumeration Value>]]; 
+0

有什么建议吗? – Chrizzz

+0

使用某种地图。要么由C++的STL映射提供(如果您知道C++),要么使用“NSArray”展开自己的映射。我用一个使用NSArray的例子更新了答案。 – gschandler

+0

是的,谢谢。我想我可以做到这一点。它仍然更容易,然后多个tableviews :-) – Chrizzz

1

gschandler是正确的,你不能那样做。从技术上讲,你可以使用一个预处理命令

#if something 
    enum 
#endif 

但真正的问题是,为什么要做到以上。你认为它会为你做什么?使用第一枚枚举集没有什么坏处。谁在乎你是否不用使用 form_email?坐在那里没有任何伤害。

+0

原因是,我想在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中使用开关。根据if语句我想使用单元格或跳过单元格。我至少有3个不同的场景和更多的10个单元。 – Chrizzz

+0

这似乎也可以。但我认为其他答案更传统。不管怎样,谢谢你。 – Chrizzz