How do I add a CGPoint to NSMutableArray?

前端 未结 6 1779
小蘑菇
小蘑菇 2021-02-01 13:58

I want to store my CGPoint to the NSMutable Array, so , I have method like this:

[self.points addObject:CGPointMake(x, y)];

But I got the error

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 14:20

    To build on the answer given by atbreuer11, you can convert your CGPoint to NSValue, store it in NSMutableArray and convert it back using the following:

    //Convert CGPoint and Store it
    CGPoint pointToConvert = CGPointMake(100.0f, 100.0f);
    NSValue *valueToStore = [NSValue valueWithCGPoint:pointToConvert];
    NSMutableArray *arrayToKeep =[NSMutableArray arrayWithObject:valueToStore];
    

    Then restore it again:

    CGPoint takeMeBack;
    for (NSValue *valuetoGetBack in arrayToKeep) {
        takeMeBack = [valuetoGetBack CGPointValue];
        //do something with the CGPoint
    }
    

    That's probably the easiest way to do it. You can write a complete class and do all types of data manipulation, but I think it would be an overkill, unless you really have to.

提交回复
热议问题