我有一个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];
}
其他类设置对NSMutableArray的引用:
lineGraph.linesToDraw = self.entityLines;
lineKey.linesForKey = self.entityLines;
答案 0 :(得分:1)
在不同的类中赋予属性(或实例变量)相同的名称不会导致它们指向相同的对象。创建数组后,需要将指向数组的指针传递给其他类实例中的属性。
@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(_lines,_otherClass)中使用ivars,但我想保持这个示例简单。