如何在swift3.0中使用nonjson数据

时间:2017-04-20 07:31:28

标签: ios uitableview swift3

我面临来自swft3.0支持的非json数据的一些问题。

这是我正在使用的Api http://api.geonames.org/export/geonamesData.js?username=orakzai

响应是:

var geonamesPostalCodeCountries = ["AD","AR","AS","AT","AU","AX","BD","BE","BG","BR","BY","CA","CH","CO","CR","CZ","DE","DK","DO","DZ","ES","FI","FO","FR","GB","GF","GG","GL","GP","GT","GU","HR","HU","IE","IN","IS","IT","JE","JP","LI","LK","LT","LU","MC","MD","MK","MP","MQ","MT","MX","MY","NC","NL","NO","NZ","PH","PK","PL","PM","PR","PT","RE","RO","RU","SE","SI","SK","SM","TH","TR","US","VA","VI","WF","YT","ZA","GR"]; var geonamesUserIpCountryCode='null';

请找到以下代码。

callApi(apiName: "http://api.geonames.org/export/geonamesData.js?username=orakzai")    

 func callApi(apiName :String) {
   let reachability = Reachability()
   if !(reachability?.isReachable)! {
       showAlertView(message: " We are unable to reach servers. Please check your network/internet connection.")
   } else {
       let url = URL(string: "\(apiName)")

       let session = URLSession.shared // or let session = URLSession(configuration: URLSessionConfiguration.default)

       if let usableUrl = url {
           let task = session.dataTask(with: usableUrl, completionHandler: { (data, response, error) in
               print(response)
               if let data = data {
                   if let stringData = String(data: data, encoding: String.Encoding.utf8) {
                       self.dataArray.append(stringData)
                       print(self.dataArray)
                   }
               }
           })
           task.resume()
       }
   }

当我使用此代码时,我的回复低于响应

["var geonamesPostalCodeCountries = [\"AD\",\"AR\",\"AS\",\"AT\",\"AU\",\"AX\",\"BD\",\"BE\",\"BG\",\"BR\",\"BY\",\"CA\",\"CH\",\"CO\",\"CR\",\"CZ\",\"DE\",\"DK\",\"DO\",\"DZ\",\"ES\",\"FI\",\"FO\",\"FR\",\"GB\",\"GF\",\"GG\",\"GL\",\"GP\",\"GT\",\"GU\",\"HR\",\"HU\",\"IE\",\"IN\",\"IS\",\"IT\",\"JE\",\"JP\",\"LI\",\"LK\",\"LT\",\"LU\",\"MC\",\"MD\",\"MK\",\"MP\",\"MQ\",\"MT\",\"MX\",\"MY\",\"NC\",\"NL\",\"NO\",\"NZ\",\"PH\",\"PK\",\"PL\",\"PM\",\"PR\",\"PT\",\"RE\",\"RO\",\"RU\",\"SE\",\"SI\",\"SK\",\"SM\",\"TH\",\"TR\",\"US\",\"VA\",\"VI\",\"WF\",\"YT\",\"ZA\",\"GR\"];\nvar geonamesUserIpCountryCode=\'null\';\n"]

我想var geonamesPostalCodeCountrietableview

1 个答案:

答案 0 :(得分:0)

假设您有以下字符串变量:

let response = "var geonamesPostalCodeCountries = [\"AD\",\"AR\",\"AS\",\"AT\",\"AU\",\"AX\",\"BD\",\"BE\",\"BG\",\"BR\",\"BY\",\"CA\",\"CH\",\"CO\",\"CR\",\"CZ\",\"DE\",\"DK\",\"DO\",\"DZ\",\"ES\",\"FI\",\"FO\",\"FR\",\"GB\",\"GF\",\"GG\",\"GL\",\"GP\",\"GT\",\"GU\",\"HR\",\"HU\",\"IE\",\"IN\",\"IS\",\"IT\",\"JE\",\"JP\",\"LI\",\"LK\",\"LT\",\"LU\",\"MC\",\"MD\",\"MK\",\"MP\",\"MQ\",\"MT\",\"MX\",\"MY\",\"NC\",\"NL\",\"NO\",\"NZ\",\"PH\",\"PK\",\"PL\",\"PM\",\"PR\",\"PT\",\"RE\",\"RO\",\"RU\",\"SE\",\"SI\",\"SK\",\"SM\",\"TH\",\"TR\",\"US\",\"VA\",\"VI\",\"WF\",\"YT\",\"ZA\",\"GR\"];\nvar geonamesUserIpCountryCode=\'null\';\n"

因为在Swift中你不能简单地使用eval(response);来评估像JavaScript一样的响应字符串(它会创建与字符串中声明的动态变量匹配的动态变量),具体如何:

let vars = response.components(separatedBy: "\n")

var geonamesPostalCodeCountriesStr = vars[0].components(separatedBy: "=")[1].trimmingCharacters(in: CharacterSet(charactersIn: " [];")).replacingOccurrences(of: "\"", with: "")
var geonamesPostalCodeCountries: [String] = geonamesPostalCodeCountriesStr.components(separatedBy: ",")

var geonamesUserIpCountryCodeStr = vars[1].components(separatedBy: "=")[1].trimmingCharacters(in: CharacterSet(charactersIn: " [];")).replacingOccurrences(of: "\'", with: "")
var geonamesUserIpCountryCode: String? = (geonamesUserIpCountryCodeStr == "null") ? nil : geonamesUserIpCountryCodeStr