因爲我正確理解你的問題,你可以在數組use [[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)
}
}
EDIT 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)
}
}
何pe它幫助你
你能解釋一下你試圖實現的更多嗎? –
只需在函數之外聲明...它被稱爲實例變量或類屬性... –
@MazelTov我知道我必須在外面聲明它,但是我不知道寫它的正確方法,因爲它是一個字典array [array [[「」:「」]] –