[英]NSMutableArray of ClLocationCoordinate2D
我正在嘗試創建然后檢索CLLocationCoordinate2D對象的數組,但由於某種原因,該數組始終為空。
我有:
NSMutableArray *currentlyDisplayedTowers;
CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude };
[currentlyDisplayedTowers addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)] ];
我也試過這個來添加數據:
[currentlyDisplayedTowers addObject:[NSValue value:&new_coordinate withObjCType:@encode(struct CLLocationCoordinate2D)] ];
無論哪種方式,[currentDisplayedTowers count]總是返回零。 什么想法可能會出錯?
謝謝!
要留在對象CLLocation
,可以創建CLLocation
實例並將其添加到可變數組中。
CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];
為了得到CLLocationCoordinate
從結構背CLLocation
,打電話coordinate
的對象。
CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];
正如SB所說,確保您的陣列已分配並初始化。
您可能還想在第二個代碼段中使用NSValue
包裝。 然后解碼就像:
NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];
你需要分配你的數組。
NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];
然后你可以使用它。 完成后請務必致電釋放或使用其他工廠方法。
我currentlyDisplayedTowers = nil
導致了所有問題。 此外,以前的init和alloc建議是必要的。 謝謝大家的幫助!
對於有此問題的其他人,如果您計划使用MapKit
,還有另一種解決方案。
(當然,我說IF的原因是因為導入一個像MapKit
這樣的模塊純粹是為了一個方便的包裝器方法可能不是最好的舉動..但是在這里你去。)
@import MapKit;
然后只需在需要時使用MapKit的坐標值包裝器:
[coordinateArray addObject:[NSValue valueWithMKCoordinate:coordinateToAdd]];
在你的例子..
[currentlyDisplayedTowers addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.