如何在其函数之外声明一个多数组,以便我可以在其函数之外使用它?我知道如何做一个常规数组和一个常规字典,但不是两个。
IhelpersCoordinates = [
[
"Latitude":IhelpersLatitude,
"Longitude": IhelpersLongitude,
"userId": IhelpersUid
]
]
上面的代码位于viewDidLoad中。我正在尝试在用户更新功能中使用它。我知道使用常规数组我必须在函数外部设置数组(example- var IhelpersCordinates = []
)。我试图弄清楚我将如何对上面的数组做同样的事情。
答案 0 :(得分:1)
正如我正确理解你的问题,你可以在数组中使用[[String:Double]]声明一个字典。
class YourClass {
var IhelpersCoordinates : [[String:Double]] = [
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
[
"Latitude": 51.02,
"Longitude": 20.04,
"userId": 124
],
]
func exampleFunc(){
print(IhelpersCoordinates[0]["Latitude"]) // this will print 53.02
print(IhelpersCoordinates.count) // this will print 2, because it's 2 elements array of dictionaries.
}
}
根据评论编辑
如果您想宣布dictionary inside an array inside an array
,请尝试使用以下代码:
class YourClass {
var IhelpersCoordinates : Array<Array<[String:Double]>> = [
[
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
],
[
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
],
],
]
func exampleFunc(){
print(IhelpersCoordinates[0][0]["Latitude"])
print(IhelpersCoordinates.count)
}
}
编辑2
class YourClass {
public var IhelpersCoordinates = Array<Array<[String:Double]>>()
func calculate() {
var element = [
[
"Latitude": 53.02,
"Longitude": 19.04,
"userId": 123
]
]
var element1 = [
[
"Latitude": 54.02,
"Longitude": 19.04,
"userId": 122
]
]
self.IhelpersCoordinates.append(element)
self.IhelpersCoordinates.append(element1)
}
func printValues() {
print(self.IhelpersCoordinates.count)
}
}
希望对你有帮助