Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 类方法中的NSMutableArray_Objective C_Ios_Class_Nsmutablearray_Class Method - Fatal编程技术网

Objective c 类方法中的NSMutableArray

Objective c 类方法中的NSMutableArray,objective-c,ios,class,nsmutablearray,class-method,Objective C,Ios,Class,Nsmutablearray,Class Method,我创建了一个类方法,它将随机位置作为NSValue对象返回。 我想将返回的位置存储在一个可变数组中,这样下次调用类方法时,它会检查可变数组中的位置,如果它发现一个位置与存储的位置太接近,就会生成一个新的位置 我似乎不明白的是如何分配和初始化可变数组以与我的类方法一起使用。我试着在class方法中分配它。但这不起作用,因为它将在每次调用时初始化,因此以前存储的值是。。。删除 然后,我想使用生成的位置在视图上放置一些圆(不重叠)。现在,随机生成的位置可以工作,但是它们重叠,因为在我的randompo

我创建了一个类方法,它将随机位置作为NSValue对象返回。 我想将返回的位置存储在一个可变数组中,这样下次调用类方法时,它会检查可变数组中的位置,如果它发现一个位置与存储的位置太接近,就会生成一个新的位置

我似乎不明白的是如何分配和初始化可变数组以与我的类方法一起使用。我试着在class方法中分配它。但这不起作用,因为它将在每次调用时初始化,因此以前存储的值是。。。删除

然后,我想使用生成的位置在视图上放置一些圆(不重叠)。现在,随机生成的位置可以工作,但是它们重叠,因为在我的randomposition类中没有正确实现可变数组

我在objective c方面还是一个初学者…一般来说编程

@implementation randomPosition

NSMutableArray *positionsArrayPlayer1;


    +(NSValue *) givePosition:(int)player forRadius: (CGFloat) radius

    {

        if (player == 1)
        {
            //set range for arc4random
            int fromNumberY = 550;
            int toNumberY = 950;

            //set range for arc4random
            int fromNumberX = 0;
            int toNumberX = 700;


            BOOL far_enough_away = NO;
            CGPoint newpoint;

            while(!far_enough_away)
            {

                newpoint = CGPointMake((arc4random()%(toNumberX-fromNumberX+1))+fromNumberX, 
                                       (arc4random()%(toNumberY-fromNumberY+1))+fromNumberY);
                far_enough_away = YES;


                for(NSValue *existing in positionsArrayPlayer1)
                {
                    NSLog(@"test");

                    CGPoint pointb = [existing CGPointValue];
                    CGFloat deltay = pointb.y-newpoint.y;
                    CGFloat deltax = pointb.x-newpoint.x;
                    CGFloat distance = sqrt(pow(deltax,2) + pow(deltay,2));

                    //fail if closer than desired radius
                    if(distance < radius )
                    {
                        far_enough_away = NO;

                        NSLog(@"test");
                        break;
                    }
                    [positionsArrayPlayer1 addObject:[NSValue valueWithCGPoint:newpoint]];

                }
                NSLog(@"%@",positionsArrayPlayer1);
            }

            return [NSValue valueWithCGPoint:newpoint];

        } else if (player == 2 ){



             //more stuff to come here....



        } else {


            NSLog(@"player invalid");

        }

        return nil;
    }
@实现位置
NSMUTABLEARRY*位置RAYPLAYER1;
+(NSValue*)给定位置:(int)玩家半径:(CGFloat)半径
{
如果(玩家==1)
{
//设置arc4random的范围
int fromNumberY=550;
int-toNumberY=950;
//设置arc4random的范围
int fromNumberX=0;
int toNumberX=700;
距离足够远=否;
CGPoint-newpoint;
而(!足够远)
{
newpoint=CGPointMake((arc4random()%(toNumberX fromNumberX+1))+fromNumberX,
(arc4random()%(toNumberY fromNumberY+1))+fromNumberY);
足够远=是;
用于(NSValue*存在于位置RAYPLAYER1中)
{
NSLog(“测试”);
CGPoint pointb=[现有CGPointValue];
CGFloat deltay=pointb.y-newpoint.y;
CGFloat deltax=pointb.x-newpoint.x;
CGFloat距离=sqrt(功率(deltax,2)+功率(deltay,2));
//如果接近所需半径,则失败
if(距离<半径)
{
足够远=没有;
NSLog(“测试”);
打破
}
[positionsArrayPlayer1添加对象:[NSValue valueWithCGPoint:newpoint]];
}
NSLog(@“%@”,位置RAYPLAYER1);
}
返回[NSValue valueWithCGPoint:newpoint];
}else if(player==2){
//更多的东西来这里。。。。
}否则{
NSLog(@“播放器无效”);
}
返回零;
}
以下是我在viewcontroller中使用的方法。我还将放置在视图上的圆形对象存储在一个单独的数组中,以便进行其他操作

- (void) positionCirclesPlayer1 
{

    radius = 70;
    CGRect position;

    for (int i = 0; i < [colors count];i++)
    {

    CGRect positionCircleInCenter = CGRectMake(self.view.frame.size.width/2-35, self.view.frame.size.height/2-35, radius, radius);

    Circle *myCircle = [[Circle alloc] initWithFrame:positionCircleInCenter radius:radius color:[colors objectAtIndex:i]];
    myCircle.label.text = [NSString stringWithFormat:@"%i", i];
    myCircle.alpha = 0;
    myCircle.userInteractionEnabled = NO;


    theNewPointPlayer1 =   [randomPosition givePosition:1 forRadius:radius];

    position = CGRectMake(theNewPointPlayer1.CGPointValue.x, theNewPointPlayer1.CGPointValue.y, 70, 70);


    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
            myCircle.frame = position; myCircle.alpha = 1;} completion:nil];

    [playerOneCircles addObject:myCircle];
    [self.view addSubview:myCircle];

    }


}
-(无效)位置圆圈播放器1
{
半径=70;
CGRect位置;
对于(int i=0;i<[颜色计数];i++)
{
CGRect POSITION CIRCLEINCENTER=CGRectMake(self.view.frame.size.width/2-35,self.view.frame.size.height/2-35,半径,半径);
Circle*myCircle=[[Circle alloc]initWithFrame:positionCircleInCenter半径:半径颜色:[颜色对象索引:i]];
myCircle.label.text=[NSString stringWithFormat:@“%i”,i];
myCircle.alpha=0;
myCircle.userInteractionEnabled=否;
newpointplayer1=[randomPosition-givePosition:1 for radius:radius];
position=CGRectMake(newpointplayer1.CGPointValue.x,newpointplayer1.CGPointValue.y,70,70);
[UIView animateWithDuration:0.4延迟:0选项:UIViewAnimationCurveEaseIn动画:^{
myCircle.frame=position;myCircle.alpha=1;}完成:nil];
[播放对象:myCircle];
[self.view addSubview:myCircle];
}
}

在学习Obj-C之前,您可能应该先了解一下C:)


请注意,使用类方法实现此功能可能不是一个很好的体系结构。

您需要的是“静态”或“单例”。或者在应用程序代理中使用一个字段。感谢您提供的示例。为什么这不是一个很好的架构?更重要的是,什么样的体系结构才是好的呢?你没有理由做这个静态的。如果要重置数组(另一个类方法?)。专用类(
PositionGenerator
)会更好。您可能可以共享对该类的访问权限。

+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
  //this is executed when the method is called for the first time
  static NSMutableArray *positionsArrayPlayer1 = nil;

  if (positionsArrayPlayer1 == nil) { //checking if this is the first time execution
     positionsArrayPlayer1 = [[NSMutableArray alloc] init];
  }

}


//global variable accessible only from this file
static NSMutableArray *positionsArrayPlayer1;

@implementation randomPosition

//this method is called when the class is loaded.
+(void)initialize {
   positionsArrayPlayer1 = [[NSMutableArray alloc] init];
}

+(NSValue*)givePosition:(int)player forRadius:(CGFloat)radius {
   //do something with the array
}

@end