2014-07-22 23 views
0

我有以下方法,其中創建一個NSMutableArray並添加一些值到它:的NSMutableArray要零

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

我然後設定爲等於通過執行予分配的相同的方法中的以下的數組的屬性並初始化所述陣列:

self.wavePoints = pointsForInterpolation; 

self.wavePoints是一個NSMutableArray屬性,它是強(@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

爲什麼你需要可變地複製你的財產?你不應該已經保留它強烈嗎? – Aaron

+0

此外,您可能需要仔細檢查並查看其他對象可能會調用'-setWavePoints:',併發送可能會意外更改'self.wavePoints'的東西... – Aaron

+0

對不起'mutableCopy'是舊代碼,應該有已被刪除。我環顧四周,無法找到任何會在我不知情的情況下調用self.wavePoints的內容。 – Brosef

回答

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循環,並且與數組無關'編輯出來。

相關問題