Removing object from NSMutableArray

前端 未结 5 1440
栀梦
栀梦 2020-12-03 21:28

I stumbled across the following shortcut in setting up a for loop (shortcut compared to the textbook examples I have been using):

for (Item *i in items){ ...         


        
5条回答
  •  生来不讨喜
    2020-12-03 21:52

    The former loop is a "for-each" loop in Objective C.

    *i is a pointer to the direct item in the items-Array (most of the time this will be NSMutableArray).

    This way you can operate directly on the item:

    [items removeObject: i];
    

    This (should) work - I am currently not working on my Mac and can't check it. However it might be that Objective-C Prevents removing objects while iterating over the collection (that is quite common in most languages).

提交回复
热议问题