سؤال

I'm trying to get a square to appear at one of the points in an array of points. I'm using 'Square.center = array[random integer]' to pick the point.

I get the error "Assigning to 'CGPoint' (aka 'struct CGPoint') from incompatible type 'id'". I think this means it can't find a point in my array.

I have my array set up like so, cobbled together from various examples I've found.

Array = [NSArray arrayWithObjects:
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x+55,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x-55,
                                              self.canvas.center.y
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x,
                                              self.canvas.center.y+55
                                              )],
        [NSValue valueWithCGPoint:CGPointMake(
                                              self.canvas.center.x-55,
                                              self.canvas.center.y
                                              )],
        nil
        ];

How can I get my square onto one of those points? Later I want to have a the same square on every point.

هل كانت مفيدة؟

المحلول

The error you are getting is indicating that it can't convert the id of the NSValue to a CGPoint. There is a similar discussion at this link:

How can I add CGPoint objects to an NSArray the easy way?

What you need to do is get the CGPoint back from the NSValue. NSValue provides the CGPointValue for this very situation. So, from the NSValue you get back from the NSArray you would call the CGPointValue method to get you the last step. If you had an NSArray named array and a C4Shape named Square you could do this:

    Square.center = [array[randomInteger] CGPointValue];

Where randomInteger would be some integer that you picked somewhere else.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top