Ios 用于存储多个值的动态代码
我有一个多边形,它是使用谷歌地图iOS SDK为一个县绘制的 对于每个县,都有多个纬度和经度,因此我们可以绘制它 代码代表1个国家/地区。有没有一种方法可以让我在多个国家使用 因此,结构将是:Ios 用于存储多个值的动态代码,ios,arrays,swift,Ios,Arrays,Swift,我有一个多边形,它是使用谷歌地图iOS SDK为一个县绘制的 对于每个县,都有多个纬度和经度,因此我们可以绘制它 代码代表1个国家/地区。有没有一种方法可以让我在多个国家使用 因此,结构将是: County 1 - lat long 32.4757 -86.41182 31.7557 -88.41482 32.4357 -87.65335 County 2 - lat long 42.4757 -96.41182 41.7557 -98.41
County 1 -
lat long
32.4757 -86.41182
31.7557 -88.41482
32.4357 -87.65335
County 2 -
lat long
42.4757 -96.41182
41.7557 -98.41482
42.4357 -97.65335
County 3 -
lat long
52.4757 -76.41182
51.7557 -78.41482
52.4357 -77.65335
代码:
选项1 您可以像这样尝试在存储lat long的
Location
struct上创建,并更改将存储Country name和CountryLocation数组的Countrystruct
struct CountryLocation {
let lat: CLLocationDegrees
let long: CLLocationDegrees
}
struct Country {
var name: String
var locations = [CountryLocation]()
}
现在创建country数组,它的所有对象将存储该特定国家的位置
选项2
您可以使用字典
,其中包含键作为国家名称,值作为国家的数组,您只需创建字典,无需更改结构中的任何内容
var countries = [String: [Country]]()
编辑:要存储选项1的对象,请尝试如下操作
var countries = [Country(name:"Ind",locations:[CountryLocation(lat:22.663696,long:87.746803),CountryLocation(lat:22.542061,long:88.318954)],
Country(name:"UK",locations:[CountryLocation(lat:52.412811,long:-1.778197),CountryLocation(lat:51.481583,long:-3.179090),CountryLocation(lat:50.768036,long:0.290472)]
]
当然有办法。数组的数组?一系列字典?某个自定义结构的数组?县名称-数组对字典?县名称词典-自定义结构对?这里有什么问题?选择一个或考虑另一个。为什么要定义位置
结构?已经有OP需要使用的CLLocationCoordinate2D
。我如何申报我的县?county(名称:“Alabama county”,locations:[Location.lat:2.4757,Location.long:-86.41182])给了我错误。请检查编辑后的答案,还有locations是CountryLocations的数组CountryLocations
你能给我一个如何在Country struct中插入数据的示例吗?还有,如何构造for循环,以便我可以用每个lat一次访问一个县,长的
var countries = [Country(name:"Ind",locations:[CountryLocation(lat:22.663696,long:87.746803),CountryLocation(lat:22.542061,long:88.318954)],
Country(name:"UK",locations:[CountryLocation(lat:52.412811,long:-1.778197),CountryLocation(lat:51.481583,long:-3.179090),CountryLocation(lat:50.768036,long:0.290472)]
]