Access objects of a specific type using for-in loop in Objective C [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-02 03:10:02

Can I do something like this...

No, you can't use the type of the index variable in fast enumeration to select only some of the objects in the collection. If the collection contains different types of objects, you'll have to test each object first. Instead of testing class membership, though, it's often nicer to test for the behavior you're looking for with -respondsToSelector: or -conformsToProtocol instead of -isKindOfClass:.

To my knowledge: no.

The for each loop will traverse every object in the collection, and I don't think you can specify that you only want to traverse a specific type.

To be clearer: The Object you specify: for (MyObject* obj){..} is a type-cast. So you're telling the object in the collection that they should be/behave as MyObject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!