How to Create 2D array in Swift?

前端 未结 3 1820
轻奢々
轻奢々 2021-01-16 06:43

Hi there I am new to Swift, I am trying to save Longitude and Latitude and place name from map\'s coordinate object to an Multidimensional array i.e:

Can anyone plea

3条回答
  •  花落未央
    2021-01-16 07:03

    Solution using an enum for Lat/Lon/Place (as you don't show us what these are):

    enum Pos {
        case Lat
        case Lon
        case Place
    
        static let allPositions = [Lat, Lon, Place]
    }
    
    var myMatrix = [[Pos:Any]]()
    myMatrix.append([.Lat: 51.130231, .Lon: -0.189201, .Place: "Home"])
    myMatrix.append([.Lat: 52.130231, .Lon: -1.189201, .Place: "Office"])
    myMatrix.append([.Lat: 42.131331, .Lon: -1.119201, .Place: "Dinner"])
    
    /* check */
    for (i,vector) in myMatrix.enumerate() {
        for pos in Pos.allPositions {
            print("myMatrix[\(i)][\(pos)] = \(vector[pos] ?? "")")
        }
    }
    /*
    myMatrix[0][Lat] = 51.130231
    myMatrix[0][Lon] = -0.189201
    myMatrix[0][Place] = Home
    myMatrix[1][Lat] = 52.130231
    myMatrix[1][Lon] = -1.189201
    myMatrix[1][Place] = Office
    myMatrix[2][Lat] = 42.131331
    myMatrix[2][Lon] = -1.119201
    myMatrix[2][Place] = Dinner    */
    

提交回复
热议问题