Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
Ios 统计Swift中MapView上的注释数_Ios_Swift_Mapkit_Mkannotation - Fatal编程技术网

Ios 统计Swift中MapView上的注释数

Ios 统计Swift中MapView上的注释数,ios,swift,mapkit,mkannotation,Ios,Swift,Mapkit,Mkannotation,在完成从数组将注释放置到地图上的循环之后,我想计算注释的数量 我的代码如下: let anCount = self.mapView.annotations?.count if (anCount > 1) { //do something } 给出错误: 必须将可选类型为“Int”的值展开为类型为的值 “Int” fixit建议会产生其他错误。计算地图注释数量的正确方法是什么 谢谢您的建议。您必须打开可选选项,例如使用if let,然后可以将

在完成从数组将注释放置到地图上的循环之后,我想计算注释的数量

我的代码如下:

let anCount = self.mapView.annotations?.count
            if (anCount > 1) {
//do something
            }
给出错误:

必须将可选类型为“Int”的值展开为类型为的值 “Int”

fixit建议会产生其他错误。计算地图注释数量的正确方法是什么


谢谢您的建议。

您必须打开可选选项,例如使用
if let
,然后可以将其与
>1
测试组合在一个
if
语句中:

if let anCount = mapView.annotations?.count, anCount > 1 {
    //do something
}
但是
annotations
不是可选的(至少在当前的iOS版本中是如此),所以您可能只需要执行以下操作:

let anCount = mapView.annotations.count
if anCount > 1 {
    //do something
}

这已经被问过很多次了:顺便说一句,注意不要只计算
注释
,因为可能有不同类型的注释。例如,包括或排除地图上的用户位置(这将导致生成一个代表您添加的系统)将突然改变此逻辑。你真的应该过滤特定类型注释的结果…谢谢你的答案,但你是否因为我花了太长时间才回答而否决了我的问题?我有很多事情要做。理论上,期权应该很简单,但在实践中,如果你还在学习,每种情况都可能不同。不,我没有否决投票。我只是建议你在发帖之前多做研究。如果你要发布一个以前被问过的问题,最好包含一两个问题的参考资料,以证明你已经研究过了。