NSMutableArray归零

时间:2014-07-22 01:56:29

标签: ios objective-c

我有以下方法创建NSMutableArray并为其添加一些值:

NSMutableArray * pointsForInterpolation = [[NSMutableArray alloc] init];
//Code to add objects to array 

然后我通过在我分配和初始化数组的相同方法中执行以下操作来设置等于数组的属性:

self.wavePoints = pointsForInterpolation;

self.wavePoints是一个NSMutableArray属性,它是Strong(@property(nonatomic,strong) NSMutableArray *wavePoints;)。然后我做以下事情:

-(void) setWavePoints:(NSMutableArray *)wavePoints
{
    _wavePoints = wavePoints;
    NSLog(@"_wavePoints count %lu", [_wavePoints count]);
}

我的NSLog显示_wavePoints具有有效计数且不为零。我继续执行一些与数组无关的其他方法,直到我到达最后一个方法:

- (UIBezierPath*) createPath 
{
    NSLog(@"Wave points count %lu", [_wavePoints count]);
    NSMutableArray* data = [self.wavePoints mutableCopy];
    NSLog(@"ARRay count 2 %lu", [data count]);
}

部首:

@interface OSWaveView ()

    @property(nonatomic,strong)AVAsset *audioAsset;
    @property(nonatomic,strong) NSMutableArray *wavePoints;
    @property (nonatomic, strong) NSMutableArray * pointsForInterpolation;
    @property(nonatomic,assign)CGFloat minAmpl;
    @property(nonatomic,assign)CGFloat maxAmpl;
    @property (nonatomic, strong) CAShapeLayer* blueWave;
    @property (nonatomic, strong) CAShapeLayer* redWave;

@end

这最后两个NSLog显示计数为零。我的初始数组(pointsForInterpolation)是否已发布?

1 个答案:

答案 0 :(得分:0)

-(void) setWavePoints:(NSMutableArray *)wavePoints {
    _wavePoints = wavePoints;
    NSLog(@"_wavePoints count %lu", [_wavePoints count]); 
}

My NSLog shows that _wavePoints has a valid count and is not nil. I go on to execute some other methods that have nothing to do with the array until I reach this last method:

- (UIBezierPath*) createPath 
{
    NSLog(@"Wave points count %lu", [_wavePoints count]);
    NSMutableArray* data = [self.wavePoints mutableCopy];
    NSLog(@"ARRay count 2 %lu", [data count]);
}

您的解释略有偏差。即使_wavePoints为零,您的日志也会返回0。你应该做一个明确的检查,看看数组是否为零。

如果正确初始化,

NSLog(@"%@", self.wavePoints)应该返回除零/零之外的其他内容。一旦确认,那么你知道数组不是问题,而是你没有向数组中添加任何值,这会将问题指向你的for循环,并且没有任何与数组有关的nil& #39;退出。