Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone ClLocationCoordinate2D的NSMutableArray_Iphone_Ios4 - Fatal编程技术网

Iphone ClLocationCoordinate2D的NSMutableArray

Iphone ClLocationCoordinate2D的NSMutableArray,iphone,ios4,Iphone,Ios4,我试图创建然后检索CLLocationCoordinate2D对象的数组,但由于某些原因,该数组总是空的 我有: NSMutableArray *currentlyDisplayedTowers; CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude }; [currentlyDisplayedTowers addObject:[NSData

我试图创建然后检索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)] ];
无论哪种方式,[currentlyDisplayedTowers count]始终返回零。你知道哪里出了问题吗


谢谢

您需要分配阵列

NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];

那你就可以用了。完成后,请务必调用release或使用其他出厂方法。

如SB所述,请确保已分配并初始化数组

您可能还希望在第二个代码段中使用
NSValue
包装。那么解码就简单到:

NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];

要留在对象区域,您可以创建
CLLocation
的实例,并将它们添加到可变数组中

CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];
要从
CLLocation
获取
CLLocationCoordinate
结构,请调用对象上的
coordinate

CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];

我的
currentlyDisplayedTowers=nil
导致了所有问题。此外,之前向init和alloc提供的建议也是必要的。谢谢大家的帮助

如果您打算使用
MapKit
,那么对于其他有此问题的人,还有另一种解决方案

(我之所以说IF,当然是因为纯粹为了方便的包装方法而导入
MapKit
之类的模块可能不是最好的做法……不过,还是这样吧。)

然后,只要在需要时使用MapKit的坐标值包装器即可:

[coordinateArray addObject:[NSValue valueWithMKCoordinate:coordinateToAdd]];
在你的例子中

[currentlyDisplayedTowers addObject:[NSValue valueWithMKCoordinate:new_coordinate]];

谢谢某人,但那似乎没什么区别。我认为我也在正确地发布和解除分配(每次添加发布一次,最后解除分配)。你不想每次添加都发布。当您不再需要对数组的引用时,只需释放它。明白了。但仍然无法使数组具有任何类型的计数。有没有想过我还可能做错了什么?这可能与我所做的一切都在一个While循环中这一事实有关?您需要显示更多的代码。添加后立即调用计数,并查看它是否>0。您可能每次都在重新创建数组,并在循环外部引用一个空数组。是的,它已全部分配和初始化。但仍然不起作用:(.分配和初始化似乎没有任何效果。我在while循环中执行所有这些操作是否有任何区别。出于某种原因,我得到“CLLocation可能不响应+initWithLatitude:经度:”尽管我已经添加了框架并将“#import”放在了标题中。对不起,这一定是一个非常非常难办的问题:)。下面是代码:
CLLocation*towerLocation=[CLLocation initWithLatitude:currentTowerLocation.latitude longitude:currentTowerLocation.longitude]
@Sina-很抱歉,这不是类方法。您需要将其称为
[[CLLocation alloc]initWithLocation:…经度:lon]。让我更新我的答案。
[currentlyDisplayedTowers addObject:[NSValue valueWithMKCoordinate:new_coordinate]];