Swift 2 .plist dictArray到远程Json数组错误

时间:2015-09-19 14:42:33

标签: ios arrays json swift

我有工作tableview和详细视图项目。但它基于本地.plist阵列系统我想将其更改为远程.json阵列系统我尝试了一些事情,但给出了错误。我需要你的帮助专家!工作代码。 TY。

import Foundation
import MapKit

struct VacationSpot {
  let identifier: Int
  let name: String
  let locationName: String
  let thumbnailName: String
  let whyVisit: String
  let whatToSee: String
  let weatherInfo: String
  let userRating: Int
  let wikipediaURL: NSURL
  let coordinate: CLLocationCoordinate2D
}

// MARK: - Support for loading data from plist

extension VacationSpot {

  static func loadAllVacationSpots() -> [VacationSpot] {
    return loadVacationSpotsFromPlistNamed("vacation_spots")
  }

  private static func loadVacationSpotsFromPlistNamed(plistName: String) -> [VacationSpot] {
    guard
      let path = NSBundle.mainBundle().pathForResource(plistName, ofType: "plist"),
      let dictArray = NSArray(contentsOfFile: path) as? [[String : AnyObject]]
      else {
        fatalError("An error occurred while reading \(plistName).plist")
    }




    var vacationSpots = [VacationSpot]()

    for dict in dictArray {
      guard
        let identifier    = dict["identifier"]    as? Int,
        let name          = dict["name"]          as? String,
        let locationName  = dict["locationName"]  as? String,
        let thumbnailName = dict["thumbnailName"] as? String,
        let whyVisit      = dict["whyVisit"]      as? String,
        let whatToSee     = dict["whatToSee"]     as? String,
        let weatherInfo   = dict["weatherInfo"]   as? String,
        let userRating    = dict["userRating"]    as? Int,
        let wikipediaLink = dict["wikipediaLink"] as? String,
        let latitude      = dict["latitude"]      as? Double,
        let longitude     = dict["longitude"]     as? Double
        else {
          fatalError("Error parsing dict \(dict)")
      }

      let wikipediaURL = NSURL(string: wikipediaLink)!
      let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      let vacationSpot = VacationSpot(
        identifier: identifier,
        name: name,
        locationName: locationName,
        thumbnailName: thumbnailName,
        whyVisit: whyVisit,
        whatToSee: whatToSee,
        weatherInfo: weatherInfo,
        userRating: userRating,
        wikipediaURL: wikipediaURL,
        coordinate: coordinate
      )

      vacationSpots.append(vacationSpot)
    }

    return vacationSpots
  }
}

1 个答案:

答案 0 :(得分:0)

使用相同的代码解决。

  let wikipediaURL = NSURL(string: wikipediaLink)!
      let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      let vacationSpot = VacationSpot(
        identifier: identifier,
        name: name,
        locationName: locationName,
        thumbnailName: thumbnailName,
        whyVisit: whyVisit,
        whatToSee: whatToSee,
        weatherInfo: weatherInfo,
        userRating: userRating,
        wikipediaURL: wikipediaURL,
        coordinate: coordinate
      )