2016-11-16 19 views
1

Swift 2.3で私は[String:AnyObject]を受け取り、それをキャストできます! [[Double]]。しかし、Swift 3.0で同じオブジェクトを使用した場合、私はそれを[[Double]]としてキャストできません。私はを通じて、[[AnyObject]]または[[Any]]として代わりにループを、それをキャストして試してみて、変換すると、私は次のエラーを取得する:配列の配列をSwift 3.0の[[Double]]としてキャストするにはどうすればよいですか?

Could not cast value of type '__NSArrayI' (0x10783ec08) to 'NSNumber' (0x106e47320).

次のコードは、私のスウィフト2.3実装で動作しますが、スウィフトはありません3.0

func extractCoordinatesForArea() { 
    // This first line is where it crashes 
    let theArraysOfCoordinates = myObject.geoArea!["geometry"]!["coordinates"] as! [[Double]] 
    var newArea:[CLLocationCoordinate2D] = [] 
    for coordArray in theArraysOfCoordinates { 
     // x is LONG, LAT 
     let x = [coordArray[0], coordArray[1]] 
     // Reverse the coordinates because they are stored as LONG, LAT on the server 
     let y = CLLocationCoordinate2DMake(x[1],x[0]) 
     print(y) 
     newArea.append(y) 
    } 
} 

エラーは意味がありますが、明示的に型を宣言したり、forループで変換したりしても、これを動作させることはできません。どんな助けでも大歓迎です。

答えて

1

このような最初の文を壊してみてください。

if let geometryDic = myObject.geoArea["geometry"] as? [String:Any], 
    let coordinatesArray = geometryDic["coordinates"] as? [Any] { 
    let theArraysOfCoordinates = coordinatesArray.first as? [[Double]] { 
    var newArea:[CLLocationCoordinate2D] = [] 
    for coordArray in theArraysOfCoordinates { 
     //There is no need to create another array X you can directly use coordArray 
     // Reverse the coordinates because they are stored as LONG, LAT on the server 
     let coordinate = CLLocationCoordinate2DMake(coordArray[1],coordArray[0]) 
     print(coordinate) 
     newArea.append(coordinate) 
    } 
} 
+0

ありがとうございました。これは多くの助けになりました! theArraysOfCoordinatesをキャストしなければならなかったか? [[AnyObject]]を作成してから、theArrayOfCoordinate = theArraysOfCoordinates [0]を作成してください! '[Double]] – stvnmcdonald

+0

' geometryDic ["coordinates"] 'は2つのネストされた配列を持つ配列を含み、あなたの応答を表示せず、編集された答えをチェックし、添え字の代わりにarrayの最初のプロパティを使う'これは、配列が空の場合にクラッシュする原因になります。 –

関連する問題