在目标C中对枚举进行迭代? [英] Iteration over enum in Objective C?
本文介绍了在目标C中对枚举进行迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
enum Colour {
white,
pink,
yellow,
blue
} Colour;
我想做这样的事情:
for (int colour in Colour){
// Do something here.
}
我可以这样做吗?如果可以,怎么办?感谢您的帮助!
Can I do this and if yes, how? Thanks for your help!
推荐答案
一个枚举来自C,而快速枚举是Objective-C 2.0的一个补充.它们无法协同工作.
an enum comes from C while fast enumeration was an addition of Objective-C 2.0.. they don't work together.
Type existingItem;
for ( existingItem in expression ) { statements }
表达式必须符合NSFastEnumeration协议,并且必须是对象!枚举的元素"不是对象.
expression must conform to the NSFastEnumeration Protocol and be an Object! "elements" of an enum are not objects.
see this link for more information Apple's Fast Enumeration Documents
检查此示例以了解快速枚举的工作原理:
check this example to see how fast enumeration works:
NSArray *array = [NSArray arrayWithObjects:
@"One", @"Two", @"Three", @"Four", nil];
for (NSString *element in array) {
NSLog(@"element: %@", element);
}
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
NSString *key;
for (key in dictionary) {
NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}
这篇关于在目标C中对枚举进行迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文