import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!{
didSet{
textField.delegate = self
textField.text = textToTranslate
}
}
@IBOutlet weak var label: UILabel!{
didSet{
print("didSet")
}
}
var textToTranslate: String?{
didSet{
TranslateTheText()
}
}
private func TranslateTheText(){
var request = URLRequest(url: URL(string: "https://translate.yandex.net/api/v1.5/tr.json/translate")!)
request.httpMethod = "POST"
let key = "trnsl.1.1.20170105T183003Z.a061e235d7abde36.51b76f0ff88678b7d1cdd254edd2951b4fb00dbf"
let postString = "key=" + key + "&text=" + textToTranslate! + "&lang=en-ru"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { [weak weakSelf = self] data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
var parsedObject: Any?
do {
parsedObject = try JSONSerialization.jsonObject(with: data,options: JSONSerialization.ReadingOptions.allowFragments)
}
catch let error {
print ("error: \(error)")
}
if let source = parsedObject as? [String: Any]{
if let text = source["text"] as? Array<String>{
for temp in text{
print(temp)
weakSelf?.label.text = temp
}
}
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
textToTranslate = textField.text
return true
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
DispatchQueue.main.async { [weak self] in
self?.label.text = temp
}
weakSelf
не стоит. просто [weak self]
отлично работает. В такой ситуации код читается легче и подсветка синтаксиса работает корректнее;if let source = parsedObject as? [String: Any], let text = source["text"] as? [String] {
for temp in text {
print(temp)
self?.label.text = temp
}
}