Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 为什么赢了';t此代码将对象放入NSMULTABLE数组中_Objective C_Object_Nsmutablearray - Fatal编程技术网

Objective c 为什么赢了';t此代码将对象放入NSMULTABLE数组中

Objective c 为什么赢了';t此代码将对象放入NSMULTABLE数组中,objective-c,object,nsmutablearray,Objective C,Object,Nsmutablearray,。。。这将显示K,但不会显示“D”和“s” 下面是Line类的代码 [self.lines addObject: [[Line alloc] initWithPosition:CGPointMake([UIScreen mainScreen].bounds.size.width, _currentHeight)]]; for (Line *line in self.lines) { NSLog(@"d"); } NSLog(@"k"); 我很抱歉,因为这个问题是非常基于代码的,但我

。。。这将显示K,但不会显示“D”和“s”

下面是Line类的代码

[self.lines addObject: [[Line alloc] initWithPosition:CGPointMake([UIScreen mainScreen].bounds.size.width, _currentHeight)]];

for (Line *line in self.lines) {
    NSLog(@"d");
}

NSLog(@"k");

我很抱歉,因为这个问题是非常基于代码的,但我似乎找不到解决它的方法。谢谢您的时间。

您必须在某个地方初始化数组,比如在视图控制器的
init
viewDidLoad
方法中

//Line.m
#import "Line.h"

@implementation Line

- (id)initWithPosition:(CGPoint)pos{

    self = [self init];
    if (self) {
        _y = pos.y;
        _x = pos.x;
    }
    return self;
}

-(void)minusFromX: (int)number{
    _x -= number;
}

@end

您是否创建了
self.lines
实例?
//Line.m
#import "Line.h"

@implementation Line

- (id)initWithPosition:(CGPoint)pos{

    self = [self init];
    if (self) {
        _y = pos.y;
        _x = pos.x;
    }
    return self;
}

-(void)minusFromX: (int)number{
    _x -= number;
}

@end
self.lines = [[NSMutableArray alloc] init];