2015-10-27 132 views
0

您好,我對swift比較陌生。我想要構建一個數組,其中包含來自下面顯示的字符串的元組數組。每個數組中元組的數量並不總是恆定的。最終的數據結構將是: [[(雙,雙)]]座標字符串解析

要解析的字符串: 「((2363448.9 5860581.3,2363438.0 5860577.9),(2363357.5 5860494.7,2363303.2 5860502.0,2363282.5 5860502.5),(2363357.5 5860494.7),(.........等))「

我想知道如果有人有任何想法,最好/最有效的方式做到這一點。我正在考慮迭代原始字符串並根據遇到的字符設置標誌。然後爲東和北建立字符串。但我不確定這是否是最好的方式,看起來過於複雜。 效率是一個因素,因爲字符串有時可能相當大。

--UPDATE 這是我到目前爲止的代碼,這似乎工作。有關改進或更好的方法的想法,讚賞,因爲代碼有點混亂。感謝迄今爲止幫助過的所有人。

typealias Coordinate = (easting: Double, northing: Double) 
typealias Coordinates = [[Coordinate]] 

var array = Array<Array<Coordinate>>() //Array of array of tuples(Double) 
var tupleArray = [Coordinate]() //Array of tuples(Double) 
var tuple: (Double, Double)! = (0, 0) 
var easting: Bool = false 
var northing: Bool = false 
var eastingString: String = "" 
var northingString: String = "" 
var openBracket = 0 
let tempString = getSubstringFromIndex(wktGeometry , character: "G") //string to parse 
for char in tempString.characters { 
    if char == "(" { 
     openBracket++ 
     if openBracket == 2 { 
      easting = true 
     } 
    } else if char == ")" { 
     openBracket-- 
     if openBracket == 1 { 
      tuple.1 = Double(northingString)! 
      tupleArray.append(tuple) 
      array.append(tupleArray) 
      tupleArray = [Coordinate]() 
      eastingString = "" 
      northingString = "" 
     } 
    } else if char == "," { 
     if openBracket == 2 { 
      tuple.1 = Double(northingString)! 
      tupleArray.append(tuple) 
      eastingString = "" 
      northingString = "" 
     } 
    } else if char == " " { 
     if easting == true { 
      tuple.0 = Double(eastingString)! 
      easting = false 
      northing = true 
     } else { 
      northing = false 
      easting = true 
     } 
    } else { 
     if easting { 
      eastingString += String(char) 
     } else if northing { 
      northingString += String(char) 
     } 
    } //end if 
} //end for 
print(array) 
+2

這幾乎是JSON。如果你交換了'(' - >'['和')' - >']',你可以使用內置的JSON解析代碼。 – trojanfoe

+0

好,非常感謝,我會看看。 – ozzyzig

+0

@trojanfoe這樣做,你將以'[[2363448.9 5860581.3,2363438.0 5860577.9],...''結尾,這將不是有效數組(缺少逗號)。 – Moritz

回答

-1

本質上[[2363448.9 5860581.3, 2363438.0 5860577.9], [2363357.5 5860494.7, 2363303.2 5860502.0], [2363282.5 5860502.5, 2363357.5 5860494.7]]會使用上面提到的trojanfoe的JSON代碼。除此之外,您可以使用一個變量來存儲它們並在使用追加時添加它們。它肯定會看起來更乾淨,除非你有一個常數。

1

你的代碼的問題在於你試圖一次做所有事情。它會得到更簡單,如果你拆分任務分解成小的子任務:

// will parse "2363448.9 5860581.3" 
func parseCoordinate(coordinateString: String) -> (Double, Double) { 
    // possible implementation: 
    // 1. split string by space, 
    // 2. assert there are two components 
    // 3. parse them to Doubles and return them 
} 

// will parse "(2363448.9 5860581.3, 2363438.0 5860577.9)" 
func parseCoordinates(coordinatesString: String) -> [(Double, Double)] { 
    // possible implementation: 
    // 1. remove last character "(" and end character ")" 
    // 2. split the string by "," + remove spaces around results 
    // 3. call parseCoordinate for every result (e.g. using "map" method) 
} 

// will parse your whole string 
func parse(string: String) -> [[(Double, Double)]] { 
    // possible implementation 
    // 1. Use a smart regular expression to split by "," not inside "(" and ")" or just by string "), (" 
    // 2. call parseCoordinates for every result (e.g. using "map" method) 
} 

最重要的事情是:

  1. 字符串分割(見https://stackoverflow.com/a/25678505/669586
  2. Array.map方法
+0

好的非常感謝我會查找鏈接和Array.map。是的,我同意我寫這篇文章的方式有點麻煩,如果所有的案例都被覆蓋了,可能會出現bug。 – ozzyzig