Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Ios 在其他类中操作NSMutableArray时未正确修改_Ios_Objective C_Ipad_Automatic Ref Counting - Fatal编程技术网

Ios 在其他类中操作NSMutableArray时未正确修改

Ios 在其他类中操作NSMutableArray时未正确修改,ios,objective-c,ipad,automatic-ref-counting,Ios,Objective C,Ipad,Automatic Ref Counting,我在视图控制器类中有一个NSMutableArray作为属性,这一行正在另外两个类中使用。其中一个类操作数组(向数组中添加更多对象),另一个类仅从数组中读取。当其中一个类操纵数组时,它不会在视图控制器类中被修改,而视图控制器类在视图控制器类中被实例化。因此,第三个类没有得到它需要的正确日期 在视图控制器类中: @property (nonatomic, strong) NSMutableArray *entityLines; 在其他两类中: @property (nonatomic, weak

我在视图控制器类中有一个NSMutableArray作为属性,这一行正在另外两个类中使用。其中一个类操作数组(向数组中添加更多对象),另一个类仅从数组中读取。当其中一个类操纵数组时,它不会在视图控制器类中被修改,而视图控制器类在视图控制器类中被实例化。因此,第三个类没有得到它需要的正确日期

在视图控制器类中:

@property (nonatomic, strong) NSMutableArray *entityLines;
在其他两类中:

@property (nonatomic, weak) NSMutableArray *linesToDraw;
@property (nonatomic, weak) NSMutableArray *linesForKey;
阵列初始化:

- (id)init
{
    self = [super init];
    if (self) {
        self.title = @"Line graph";
        lineQuery = [[LineGraphQuery alloc] init];
        entityLines = [NSMutableArray array];
    }

    return self;
}
修改数组:

     - (void)drawRect:(CGRect)rect
{
    if(data.namedEntityData.count > 0) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, LINE_WIDTH);
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
        CGContextFillRect(context, rect);

        [self clearAllLines];
        for(NSString *key in [data.namedEntityData allKeys]) {
            EntityLine *entityLine = [self getNamedEntityLineForName:key];
            if(!entityLine) {
                entityLine = [[EntityLine alloc] init];
                entityLine.name = key;
                entityLine.color = [self getRandomColor];
            }
            float intervalX = STARTING_INTERVAL_X;
            float lastRangeY = MIN_EVENT_COUNT_Y;

            CGContextSetStrokeColorWithColor(context, [entityLine.color CGColor]);
            NSArray *events = [data.namedEntityData objectForKey:key];
            NSInteger rangeDifference = data.endYear - data.beginYear;

            for(int i = 0; i < numberOfDateRangeIntervals; i++) {
                int startYearRange = data.beginYear + (i * (rangeDifference / numberOfDateRangeIntervals));
                int endYearRange = (i == numberOfDateRangeIntervals - 1) ? data.endYear : data.beginYear + ((i + 1) * (rangeDifference / numberOfDateRangeIntervals) - 1);
                int eventCount = [self getCountForEvents:events withBeginYear:startYearRange andEndYear:endYearRange];

                Line *line = [[Line alloc] init];
                line.begin = CGPointMake(intervalX, lastRangeY);
                CGContextMoveToPoint(context, line.begin.x, line.begin.y);
                intervalX += intervalXIncrement;
                lastRangeY = [self getYCoordinateForEventCount:eventCount];
                line.end = CGPointMake(intervalX, lastRangeY);
                [entityLine addLine:line];

                CGContextAddLineToPoint(context, line.end.x, line.end.y);
                CGContextStrokePath(context);
            }
            [linesToDraw addObject:entityLine];
        }

        [self drawEventCountLabelsWithContext:context];
        [self drawDateRangeLabelsWithContext:context];
    }
}

- (void)clearAllLines
{
    for(EntityLine *line in linesToDraw)
        [line clearLines];
}
在不同的类中为属性(或实例变量)指定相同的名称不会导致它们指向相同的对象。创建数组后,需要将指向该数组的指针传递给其他类实例中的属性

@interface ABCFirstClass ()

@property (nonatomic, strong) NSMutableArray *lines;
@property (nonatomic, strong) ABCAnotherClass *otherClass;  // Also has a property named "lines".

@end


@implementation ABCFirstClass

- (id)init
{
    self = [super init];
    if (self) {
        self.lines = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
        self.otherClass = [[ABCAnotherClass alloc] init];
        self.otherClass.lines = self.lines;
          // Now both classes have a pointer to the same array object.
    }
    return self;
}
这不需要在-init方法中发生。可能是一个完全不同的类从一个类获取指针并将其传递给另一个类


注意,我通常直接在-init(_行,_otherClass)中使用IVAR,但我想让这个示例保持简单。

发布获取数组和修改数组的代码。让我们先看看明显的检查:确保所有指针都指向同一个对象且不为零。您意识到在不同的类中为对象赋予相同的名称会创建不同的对象吗?您需要将对象指针传递给其他两个对象,以便它们具有对数组的引用。我没有看到这种情况发生在这里。@user1530580:更确切地说,它创建了单独的变量。具有相同名称的两个变量没有内在的相关性。你需要展示这些是如何设置的,因为听起来这就是问题所在。请,请,请,在你尝试任何更多的Objective-C编程之前,先研究一下对象和指针。到目前为止,您只是抄袭示例,并不真正了解自己在做什么。一般来说,对象没有名称。变量有名称,两个同名的不同变量本质上并不指向同一个对象。你是对的,我在这里选错了词。我正在做与上面例子相同的事情。我甚至更改了它,以便所有不同的NSMutableArray现在都有不同的名称。还是一样的问题。lineGraph.linesToDraw=self.entityLines;当您将数组传递给其他对象时,它是否存在?不知何故,我怀疑您没有提取otherClass属性并将其用作ABCAnotherClass对象。
@interface ABCFirstClass ()

@property (nonatomic, strong) NSMutableArray *lines;
@property (nonatomic, strong) ABCAnotherClass *otherClass;  // Also has a property named "lines".

@end


@implementation ABCFirstClass

- (id)init
{
    self = [super init];
    if (self) {
        self.lines = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
        self.otherClass = [[ABCAnotherClass alloc] init];
        self.otherClass.lines = self.lines;
          // Now both classes have a pointer to the same array object.
    }
    return self;
}