完成处理程序swift 3从函数返回一个变量

时间:2017-05-23 10:02:38

标签: ios swift swift3 completionhandler

我对swift 3中完成处理程序的语法感到困惑。

在下面的函数中,在从Web服务调用解析xml文件后,它应该返回一个变量(array [String:String])。 我的尝试在下面,但显然是不正确的。

  enum HistoryKey {
  case success([String:String])
  case failure(String)
 }

 private func getHistoryKeys(searchterm: String, completion: @escaping () -> HistoryKey) {
    let url = PubmedAPI.createEsearchURL(searchString: searchterm)
    let request = URLRequest.init(url: url as URL)
    let task = session.dataTask(with: request) { (data, response, error) in

        if let theData = data{
            let myParser = XMLParser.init(data: theData)
            myParser.delegate = self
            myParser.parse()
        }
    }
    task.resume()

    if keys.isEmpty {
        return .failure("no historyKeyDictionary")
    }else{
        return .success(keys)
    }

}// End of func

我想使用此功能如下

 let result = self.getHistoryKeys(searchTerm)

6 个答案:

答案 0 :(得分:12)

两个问题:

  • 完成处理程序传递HistoryKey实例并且没有返回值,因此签名必须相反。
  • 完成处理程序的调用必须数据任务的完成块内。

为了能够在完成块之外解析收到的数据,请在成功时返回data

enum ConnectionResult {
   case success(Data)
   case failure(Error)
}

private func getHistoryKeys(searchterm: String, completion: @escaping (ConnectionResult) -> ()) {
   let url = PubmedAPI.createEsearchURL(searchString: searchterm)
   let task = session.dataTask(with: url) { (data, response, error) in
       if let error = error {
          completion(.failure(error))
       } else {
          completion(.success(data!))
       }
  }
  task.resume()
}

并将其命名为

getHistoryKeys(searchterm: String) { connectionResult in 
    switch connectionResult {
       case .success(let data): 
           let myParser = XMLParser(data: data)
           myParser.delegate = self
           myParser.parse()
           // get the parsed data from the delegate methods

       case .failure(let error): print(error)
    }
}

答案 1 :(得分:2)

您没有使用completion阻止 使用它像:

private func getHistoryKeys(searchterm: String, completion: @escaping (_ keys: Array) -> Void) {
    //do the magic
    completion(keys)
}

然后您可以将此功能称为:

getHistoryKeys(searchterm: "str") { (keys) in 
    print("\(keys)")
}

答案 2 :(得分:1)

将结果作为参数返回到完成处理程序中:

private func getHistoryKeys(searchterm: String, completion: @escaping (result: HistoryKey) -> Void) {
    let url = PubmedAPI.createEsearchURL(searchString: searchterm)
    let request = URLRequest.init(url: url as URL)
    let task = session.dataTask(with: request) { (data, response, error) in

        if let theData = data{
            let myParser = XMLParser.init(data: theData)
            myParser.delegate = self
            myParser.parse()
        }

        //DispatchQueue.main.async { // if you want to do UI stuff dispatch calls to completion() on the main queue
        if keys.isEmpty {
            completion(.failure("no historyKeyDictionary"))
        } else{
            completion(.success(keys))
        }
        //}
    }
    task.resume()   
}

并称之为:

getHistoryKeys("searchMe") { (result: HistoryKey) in
    print(result)
}

答案 3 :(得分:0)

从我所看到的,它应该是

private func getHistoryKeys(searchterm: String, completion: @escaping (HistoryKey) -> ())

还应使用completion(.failure("no historyKeyDictionary"))completion(.success(keys))代替return

答案 4 :(得分:0)

 enum HistoryKey {
    case success([String: String])
    case failure(String)
 }

 private func getHistoryKeys(searchterm: String, completion: @escaping (_ result: HistoryKey) -> Void) {
    let url = PubmedAPI.createEsearchURL(searchString: searchterm)
    let request = URLRequest.init(url: url as URL)
    let task = session.dataTask(with: request) { (data, response, error) in

        if let theData = data{
            let myParser = XMLParser.init(data: theData)
            myParser.delegate = self
            myParser.parse()
        }

        if keys.isEmpty {
            completion(.failure("no historyKeyDictionary"))
        } else {
            completion(.success(keys))
        }
    }
    task.resume()    
} // End of func

这样的事情。更改@escaping声明并完成而不是返回。

希望它有所帮助。

答案 5 :(得分:0)

Swift 4.2

enum HistoryKey {
    case success([String:String])
    case failure(String)
}

func myFunction(str: String, completionHandler: @escaping (HistoryKey) -> ()){
     completion(.success([String:String]))
     //OR
     completion(.failure(""))
}

myFunction(str: String) { result in 
    switch result {
       case .success(let data): break;
       case .failure(let error): break;
    }
}

OR

func myFunction(str: String, completionHandler: @escaping (String) -> ()){
     completionHandler("")
} 

myFunction(str: "someThing", completionHandler: {(str) in

})