iPhone Objective-C的foreach设计 [英] iPhone Objective-C foreach design
问题描述
然后,我想列出 触摸类:作为一个接口,有几个类继承自它:
MoveTouch
class
JumpTouch
class
InterfaceTouch
class
触摸
对象。然后,我希望能够删除所有的 MoveTouch
对象(而不是其他的),然后所有的 JumpTouch
分开的对象等,在这个大列表中。不幸的是, for(MoveTouch * t touches)
不会做我想要的;
for(Touch * t touches){
if([t isKindOfClass:[MoveTouch class]]){
MoveTouch * mt =(MoveTouch *)t;
//做你想做的事情,用
I'm trying to implement a design as follows:
Touch
class : acts as an interface, several classes inherit from it:
MoveTouch
class
JumpTouch
class
InterfaceTouch
class
Then I want to have a list of Touch
objects. I then want to be able to strip out all the MoveTouch
objects ONLY (not the other ones), and then all the JumpTouch
objects separately, etc., out of this big list. Unfortunately, for (MoveTouch* t in touches)
does not do what I want; everything in the entire list gets acted upon.
解决方案 You need to test for the class type:
for (Touch *t in touches) {
if ([t isKindOfClass:[MoveTouch class]]) {
MoveTouch *mt = (MoveTouch *)t;
// do what you want with mt
}
}
这篇关于iPhone Objective-C的foreach设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!