真实设备上Swift中下标的模糊使用

时间:2016-06-03 12:06:15

标签: ios json swift google-maps

我的项目之前运行正常,如果使用模拟器仍然运行正常。但当我连接iPhone并尝试运行项目时,我收到此错误:“检索JSON信息时”下划线的模糊使用...

这是我的示例代码。

     let data = NSData(contentsOfURL: url!)

            let json=
                    NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

            routesInfo = json.objectForKey("routes")

            for i in 0 ..< routesInfo!.count
            {
                let routes = json.objectForKey("routes")![i]    //ERROR_LINE
                let overview = routes!.objectForKey("overview_polyline")

                let point = overview!.objectForKey("points")

                let path = GMSPath(fromEncodedPath: (point as? String)!)
                let polyline = GMSPolyline(path: path)

                polyline.strokeWidth = 6.0
                polyline.map = mapView
            }

Google地图返回的完整JSON是:

 {
  "geocoded_waypoints" : [
  {
     "geocoder_status" : "OK",
     "place_id" : "ChIJRVR9ciHP5zsRsRVN9_E-fb0",
     "types" : [ "premise" ]
  },
  {
     "geocoder_status" : "OK",
     "place_id" : "ChIJpapok9jO5zsRIL5b1p9GhF4",
     "types" : [ "route" ]
  }
 ],
 "routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 19.024652,
           "lng" : 72.8565404
        },
        "southwest" : {
           "lat" : 19.0176492,
           "lng" : 72.84871849999999
        }
     },
     "copyrights" : "Map data ©2016 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "2.0 km",
              "value" : 1992
           },
           "duration" : {
              "text" : "6 mins",
              "value" : 371
           },
           "end_address" : "King's Cir Flyover, Parsi Colony, Matunga, Mumbai, Maharashtra 400014, India",
           "end_location" : {
              "lat" : 19.024652,
              "lng" : 72.8531031
           },
           "start_address" : "Dheeraj Enclave, Wadala West, Wadala, Mumbai, Maharashtra 400031, India",
           "start_location" : {
              "lat" : 19.0176492,
              "lng" : 72.8561606
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "41 m",
                    "value" : 41
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 9
                 },
                 "end_location" : {
                    "lat" : 19.0178793,
                    "lng" : 72.8558529
                 },
                 "html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eRd Number 19\u003c/b\u003e toward \u003cb\u003eBalaram Babu Khedekar Marg\u003c/b\u003e",
                 "polyline" : {
                    "points" : "ikasB_vt{Lm@|@"
                 },
                 "start_location" : {
                    "lat" : 19.0176492,
                    "lng" : 72.8561606
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "85 m",
                    "value" : 85
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 28
                 },
                 "end_location" : {
                    "lat" : 19.0182834,
                    "lng" : 72.8565404
                 },
                 "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eBalaram Babu Khedekar Marg\u003c/b\u003e",
                 "maneuver" : "turn-right",
                 "polyline" : {
                    "points" : "wlasBatt{Lo@yAUa@IM"
                 },
                 "start_location" : {
                    "lat" : 19.0178793,
                    "lng" : 72.8558529
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "82 m",
                    "value" : 82
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 22
                 },
                 "end_location" : {
                    "lat" : 19.0187209,
                    "lng" : 72.85590959999999
                 },
                 "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e after Excise &amp; Customs Residential Colony (on the left)",
                 "maneuver" : "turn-left",
                 "polyline" : {
                    "points" : "goasBkxt{Lg@x@Q`@]`@"
                 },
                 "start_location" : {
                    "lat" : 19.0182834,
                    "lng" : 72.8565404
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.2 km",
                    "value" : 244
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 40
                 },
                 "end_location" : {
                    "lat" : 19.0199158,
                    "lng" : 72.85405589999999
                 },
                 "html_instructions" : "At the roundabout, continue straight onto \u003cb\u003eLady Jehangir Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "_rasBmtt{L?@@@?@@??@?@?@@??@?@?@?@A??@?@?@A@?@A@A@?@A?A@A?A@A?A??@A?A?A?A?AAaCdEuAbC"
                 },
                 "start_location" : {
                    "lat" : 19.0187209,
                    "lng" : 72.85590959999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.3 km",
                    "value" : 273
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 46
                 },
                 "end_location" : {
                    "lat" : 19.0212656,
                    "lng" : 72.8521094
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e2nd\u003c/b\u003e exit and stay on \u003cb\u003eLady Jehangir Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "oyasB{ht{L@@@B@B?F@D?F?DAHAFCFADADCBGHEFKHGDGBIBI@K?ICkAxBeBrC"
                 },
                 "start_location" : {
                    "lat" : 19.0199158,
                    "lng" : 72.85405589999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.3 km",
                    "value" : 298
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 88
                 },
                 "end_location" : {
                    "lat" : 19.0226609,
                    "lng" : 72.84999250000001
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e2nd\u003c/b\u003e exit and stay on \u003cb\u003eLady Jehangir Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "}absBu|s{LB@@BB@@B@B?@@B@B?B?B?B?B?BABABADCDA@C@A@C@C@C@A?C@C?C?C?CACAA?CACA}AhC[h@m@jA}@~A"
                 },
                 "start_location" : {
                    "lat" : 19.0212656,
                    "lng" : 72.8521094
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.2 km",
                    "value" : 233
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 50
                 },
                 "end_location" : {
                    "lat" : 19.020958,
                    "lng" : 72.84870769999999
                 },
                 "html_instructions" : "At \u003cb\u003eDr GM Phadke Chowk\u003c/b\u003e, take the \u003cb\u003e1st\u003c/b\u003e exit onto \u003cb\u003eSir Bhalchandra Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "sjbsBmos{L\\VtCpB~CtB"
                 },
                 "start_location" : {
                    "lat" : 19.0226609,
                    "lng" : 72.84999250000001
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.1 km",
                    "value" : 124
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 39
                 },
                 "end_location" : {
                    "lat" : 19.0202794,
                    "lng" : 72.84958060000001
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e1st\u003c/b\u003e exit onto \u003cb\u003eKhareghat Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "_`bsBmgs{L@A@??A@A@A@?@A@?@A@?@?@?@?@?@?@??@@?zAoCJS"
                 },
                 "start_location" : {
                    "lat" : 19.020958,
                    "lng" : 72.84870769999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.6 km",
                    "value" : 612
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 49
                 },
                 "end_location" : {
                    "lat" : 19.024652,
                    "lng" : 72.8531031
                 },
                 "html_instructions" : "\u003cb\u003eKhareghat Rd\u003c/b\u003e turns \u003cb\u003eleft\u003c/b\u003e and becomes \u003cb\u003eKing's Cir Flyover\u003c/b\u003e",
                 "polyline" : {
                    "points" : "w{asB{ls{LSMsGoE}@m@_Aq@eAw@y@o@y@o@_@Yg@a@o@g@m@g@q@o@q@m@"
                 },
                 "start_location" : {
                    "lat" : 19.0202794,
                    "lng" : 72.84958060000001
                 },
                 "travel_mode" : "DRIVING"
              }
           ],
           "traffic_speed_entry" : [],
           "via_waypoint" : []
        }
     ],
     "overview_polyline" : {
        "points" : "ikasB_vt{Lm@|@o@yA_@o@y@zA]b@BB?D?HCHOHE?cCbEsAdCDTC^KVMPSNQFU@ICkAxBeBrCB@DDBFBL?NIRKFMDOAMEyBrDkBjDrDhC`DrBBCDCHAH@fBcDgH}E}B_B_CgBqDsCqCeC"
     },
     "summary" : "Lady Jehangir Rd and King's Cir Flyover",
     "warnings" : [],
     "waypoint_order" : []
  },
  {
     "bounds" : {
        "northeast" : {
           "lat" : 19.024652,
           "lng" : 72.8565404
        },
        "southwest" : {
           "lat" : 19.0172015,
           "lng" : 72.8474793
        }
     },
     "copyrights" : "Map data ©2016 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "2.3 km",
              "value" : 2260
           },
           "duration" : {
              "text" : "7 mins",
              "value" : 392
           },
           "end_address" : "King's Cir Flyover, Parsi Colony, Matunga, Mumbai, Maharashtra 400014, India",
           "end_location" : {
              "lat" : 19.024652,
              "lng" : 72.8531031
           },
           "start_address" : "Dheeraj Enclave, Wadala West, Wadala, Mumbai, Maharashtra 400031, India",
           "start_location" : {
              "lat" : 19.0176492,
              "lng" : 72.8561606
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "41 m",
                    "value" : 41
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 9
                 },
                 "end_location" : {
                    "lat" : 19.0178793,
                    "lng" : 72.8558529
                 },
                 "html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eRd Number 19\u003c/b\u003e toward \u003cb\u003eBalaram Babu Khedekar Marg\u003c/b\u003e",
                 "polyline" : {
                    "points" : "ikasB_vt{Lm@|@"
                 },
                 "start_location" : {
                    "lat" : 19.0176492,
                    "lng" : 72.8561606
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "85 m",
                    "value" : 85
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 28
                 },
                 "end_location" : {
                    "lat" : 19.0182834,
                    "lng" : 72.8565404
                 },
                 "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eBalaram Babu Khedekar Marg\u003c/b\u003e",
                 "maneuver" : "turn-right",
                 "polyline" : {
                    "points" : "wlasBatt{Lo@yAUa@IM"
                 },
                 "start_location" : {
                    "lat" : 19.0178793,
                    "lng" : 72.8558529
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "82 m",
                    "value" : 82
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 22
                 },
                 "end_location" : {
                    "lat" : 19.0187209,
                    "lng" : 72.85590959999999
                 },
                 "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e after Excise &amp; Customs Residential Colony (on the left)",
                 "maneuver" : "turn-left",
                 "polyline" : {
                    "points" : "goasBkxt{Lg@x@Q`@]`@"
                 },
                 "start_location" : {
                    "lat" : 19.0182834,
                    "lng" : 72.8565404
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.2 km",
                    "value" : 244
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 40
                 },
                 "end_location" : {
                    "lat" : 19.0199158,
                    "lng" : 72.85405589999999
                 },
                 "html_instructions" : "At the roundabout, continue straight onto \u003cb\u003eLady Jehangir Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "_rasBmtt{L?@@@?@@??@?@?@@??@?@?@?@A??@?@?@A@?@A@A@?@A?A@A?A@A?A??@A?A?A?A?AAaCdEuAbC"
                 },
                 "start_location" : {
                    "lat" : 19.0187209,
                    "lng" : 72.85590959999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.7 km",
                    "value" : 661
                 },
                 "duration" : {
                    "text" : "2 mins",
                    "value" : 116
                 },
                 "end_location" : {
                    "lat" : 19.0172015,
                    "lng" : 72.84873309999999
                 },
                 "html_instructions" : "At the roundabout, take the \u003cb\u003e1st\u003c/b\u003e exit onto \u003cb\u003eMancherji Joshi Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eGo through 1 roundabout\u003c/div\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "oyasB{ht{L@@@B@B?F@D?F?DAHAFCFADADCBGHEF`AlBdDvGjAhC|@lBrBbF@?@?@?@??@@?@??@@??@@??@@??@?@@??@?@?@?@?@?@?@A??@?@A?~@fA"
                 },
                 "start_location" : {
                    "lat" : 19.0199158,
                    "lng" : 72.85405589999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "92 m",
                    "value" : 92
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 34
                 },
                 "end_location" : {
                    "lat" : 19.0175873,
                    "lng" : 72.8479641
                 },
                 "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eDr Babasahed Ambedkar Rd\u003c/b\u003e/\u003cb\u003eTilak Rd\u003c/b\u003e",
                 "maneuver" : "turn-right",
                 "polyline" : {
                    "points" : "ohasBqgs{Ls@nAYhA"
                 },
                 "start_location" : {
                    "lat" : 19.0172015,
                    "lng" : 72.84873309999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.4 km",
                    "value" : 431
                 },
                 "duration" : {
                    "text" : "2 mins",
                    "value" : 93
                 },
                 "end_location" : {
                    "lat" : 19.0203431,
                    "lng" : 72.8494846
                 },
                 "html_instructions" : "At \u003cb\u003eKhodadad Cir\u003c/b\u003e, take the \u003cb\u003e3rd\u003c/b\u003e exit onto \u003cb\u003eDr Baba Saheb Ambedkar Rd\u003c/b\u003e",
                 "maneuver" : "roundabout-left",
                 "polyline" : {
                    "points" : "}jasBwbs{LBBBD@DBD@D?F@D?DADADADADCBCBEBCBC@C@C@C?A@C?C?C?GAGAGCECEEECEGCEACACACAC?EAC?CAC_MiI"
                 },
                 "start_location" : {
                    "lat" : 19.0175873,
                    "lng" : 72.8479641
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.6 km",
                    "value" : 624
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 50
                 },
                 "end_location" : {
                    "lat" : 19.024652,
                    "lng" : 72.8531031
                 },
                 "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at \u003cb\u003eDattray Shankar Nadkarni Chowk\u003c/b\u003e onto \u003cb\u003eKing's Cir Flyover\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the left\u003c/div\u003e",
                 "maneuver" : "turn-right",
                 "polyline" : {
                    "points" : "c|asBgls{LJSSMsGoE}@m@_Aq@eAw@y@o@y@o@_@Yg@a@o@g@m@g@q@o@q@m@"
                 },
                 "start_location" : {
                    "lat" : 19.0203431,
                    "lng" : 72.8494846
                 },
                 "travel_mode" : "DRIVING"
              }
           ],
           "traffic_speed_entry" : [],
           "via_waypoint" : [
              {
                 "location" : {
                    "lat" : 19.0175605,
                    "lng" : 72.84923569999999
                 },
                 "step_index" : 4,
                 "step_interpolation" : 0.8934055159711098
              }
           ]
        }
     ],
     "overview_polyline" : {
        "points" : "ikasB_vt{Lm@|@o@yA_@o@y@zA]b@BB?D?HCHOHE?cCbEsAdCDTC^KVMPfFdKhCvFtBbF@?D@B@DJAJ?@A?~@fAs@nAYhABBDJFXEVOPOHM@[GWUIQCQAC_MiIJSSMqI}FeCiB{D{C}AoAcB}A"
     },
     "summary" : "King's Cir Flyover",
     "warnings" : [],
     "waypoint_order" : []
  }
  ],
  "status" : "OK"
}

1 个答案:

答案 0 :(得分:0)

你的问题是你只是强迫展开事物并最终得到AnyObject个引用,所以Swift无法真正验证你要求它做的事情。

我建议您在防御性方面编码:

if let routesInfo = json["routes"] as? [[String:AnyObject]] { 
    for routes in routesInfo { 
        if let overview = routes["overview_polyline"] as? [String:AnyObject] {
            if let point = overview["points"] as? String {
                let path = GMSPath(fromEncodedPath: point)
                let polyline = GMSPolyline(path: path)
                polyline.strokeWidth = 6.0
                polyline.map = mapView
            }
        }
    }
}