在for循环

时间:2018-04-07 09:37:52

标签: ios swift google-maps-api-2 yandex-api

我想将位置地址的googleMap Api结果翻译为波斯语。我正在使用YandexApi来翻译地址。使用“,”分隔地址组件,并将此数组传递给api。我的问题是在每次服务调用后我的userInterface都更新了。怎么能解决这个问题。  例如,当我将此数组传递给方法时:[“road”,“avenue”]  每个翻译响应后地址标签文本更新 label =“جاده”的第一个文字 标签的最后一个文字=“جاده,خیابان” 有我的代码:

func translateText(text:[String],closure:@escaping ((_ success:String?,_ error:Error?) -> Void)) {

    var translateString: String = ""
    var responseError: Error?
    for item in text {
      myGroup.enter()
    let urlString = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20171105T134956Z.795c7a0141d3061b.dc25bae76fa5740b2cdecb02396644dea58edd24&text=\(item)&lang=fa&format=plain&options=1"
    if let allowString = Utilities.shareInstance.getQueryAllowedString(url: urlString) {
      if let url = URL(string:allowString){
        Alamofire.request(url).responseJSON { response in
          guard let responseData = response.data else {
            return
          }
          do {
            let json = try JSONSerialization.jsonObject(with: responseData, options: [])
            if let res = json as? [String:Any] {
              if let code = res["code"] as? Int {
                if code == 200 {
                  if let textArr = res["text"] as? [AnyObject] {
                    let flattArr = Utilities.shareInstance.flatStringMapArray(textArr)
                    if flattArr.count > 0 {
                      translateString += " ، " + flattArr[0]
                    }
                  }
                }
              }
            }
          }catch {
            responseError = error
          }
          self.myGroup.leave()
          closure(translateString, responseError)
        }
      }
    }
   }
  } 

 private func translatingAddressArrayForChecking(address:[String])  {

    var addressString = ""
    let addressComponent = address
    if addressComponent.count > 0 {
      YandexTranslateAPI.shateInstance.translateText(text: addressComponent, closure:{ success,_ in
        if let res = success {
          addressString = res
          OperationQueue.main.addOperation {
            self.layoutActivity(start: false)
            if self.searchState == .Origin && self.destinationButton.isHidden == false{

              if let selectOrigin = self.originSelectedCity {
                if let city = selectOrigin.city_title {
                  if addressString.contains(city) {
                    self.originAddressLabel.text = addressString
                    if let location = self.startCoordinate {
                    self.setOrigin(location: location)

                    }
                  }else {
                    self.CreateCustomTopField(text: "wrong city", color: Constants.ERROR_COLOR)
                  }
                }
              }

            }else if self.searchState == .Destination || self.destinationButton.isHidden == true  {
              if let selectOrigin = self.destinationSelectedCity {
                if let city = selectOrigin.city_title {
                  if addressString.contains(city) {
                    self.destinationAddressLabel.text = addressString
                    if let location = self.endCoordinate {
                      self.setDestination(location: location)
                    }
                  }else {
                    self.CreateCustomTopField(text: "wrong city", color: Constants.ERROR_COLOR)
                  }
                }
              }
            }
          }
        }
      })

    }

使用数组组件解决标签文本更新倍数。

0 个答案:

没有答案