iPhone Objective-C的foreach设计 [英] iPhone Objective-C foreach design

查看:129
本文介绍了iPhone Objective-C的foreach设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



触摸类:作为一个接口,有几个类继承自它:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆