2012-03-27 87 views
0
target_locations[ 0] = [[CLLocation alloc] initWithLatitude : 51.50373056 
                 longitude : 0.129986111]; 
    [target_locations[ 0] release]; 

考慮上面的代碼,它是保持指定對象的保留計數爲1的正確方法嗎?NSMutableArray保留計數

*假設ARC未激活。

+1

答案取決於'target_locations'是什麼類型(儘管你不應該將'release'發送給數組中的對象)。 'NSArray'還是純C數組? – 2012-03-27 18:49:24

+2

想* owhnership *,而不是*保留計數*。或者,如果你必須,不要讓@ bbum知道。 – 2012-03-27 18:54:20

+0

target_locations是一個NSArray,希望bbum會發表評論... :) – Stanley 2012-03-27 19:01:28

回答

1

鑑於target_locationsNSMutableArray,並沒有啓用ARC,正確的方法在這裏如下:

CLLocation * newLocation = [[CLLocation alloc] initWithLatitude : 51.50373056 
                 longitude : 0.129986111]; 
target_locations[0] = newLocation; 
[newLocation release]; 

你不應該送release到數組訪問的結果,因爲你不」通過該指針擁有該對象。雖然在這種情況下工作,但它的語義不正確,如果您習慣了這種情況,可能會導致問題。

另外,考慮將target_locations重命名爲targetLocations,這與Cocoa風格一致。使用下劃線使它看起來像一個普通的C數組而不是一個對象。