Search code examples
iosxcodesemantics

"countByEnumeratingWithState:objects:count:" Error


I am trying to load a view controller where the accelerometer is moving an image view. the error in the title shows and the app crashes upon view loading.

- (void)collsionWithWalls {

    CGRect frame = self.Mover.frame;
    frame.origin.x = self.currentPoint.x;
    frame.origin.y = self.currentPoint.y;

    for (UIView *image in self.wall) {

        if (CGRectIntersectsRect(frame, image.frame)) {

            // Compute collision angle
            CGPoint MoverCenter = CGPointMake(frame.origin.x + (frame.size.width / 2),
                                               frame.origin.y + (frame.size.height / 2));
            CGPoint imageCenter  = CGPointMake(image.frame.origin.x + (image.frame.size.width / 2),
                                               image.frame.origin.y + (image.frame.size.height / 2));
            CGFloat angleX = MoverCenter.x - imageCenter.x;
            CGFloat angleY = MoverCenter.y - imageCenter.y;

            if (abs(angleX) > abs(angleY)) {
                _currentPoint.x = self.previousPoint.x;
                self.MoverXVelocity = -(self.MoverXVelocity / 2.0);
            } else {
                _currentPoint.y = self.previousPoint.y;
                self.MoverYVelocity = -(self.MoverYVelocity / 2.0);
            }

        }

    }

}

The error shows on the line: for (UIView *image in self.wall) {

Please help!


Solution

  • The for-in statement syntax:

    for (UIView *image in self.wall) ...
    

    should be read as "for each object, which we'll treat as a pointer to UIView and call image, in the collection self.wall, do…". From what you say, self.wall is not a collection. It's a UIImageView. That's the cause of the error. You're writing a statement which requires a collection, but you're not providing a collection.

    So, why are you using a for loop here? There's nothing to loop over. Did you mean to use some other expression which would evaluate to a collection instead of self.wall?