'configureLabels()'라는 함수를 작성했습니다. 'getLabels()'함수는 'GET'요청을하고 레이블 텍스트로 설정된 값을 검색한다고 가정합니다. 요청은 비동기 적이므로 요청이 완료 될 때 이스케이프 클로저를 사용하여 UI를 업데이트 할 수 있다고 생각했습니다. 나는 상대적으로 코딩에 익숙하지 않아, 내가 뭘 잘못했는지 확신 할 수 없다. 나는 이것을 알아내는 데 도움이되는 사람의 도움에 정말 감사 할 것입니다.이탈 및 비동기 호출을 이스케이프 처리하는 UI를로드하는 방법은 무엇입니까?
이import UIKit
수입 SwiftyJSON이 클래스 ItemDetailViewController이
: UIViewController에가 {@IBOutlet weak var numberOfFiberGrams: UILabel!
var ndbnoResults = [JSON]()
var ndbno = ""
let requestManager = RequestManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
configureLabels()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func configureLabels() {
requestManager.ndbnoRequest(ndbno: ndbno) { (results) in
let json = JSON(results)
let fiber = json["food"]["nutrients"][7].dictionaryValue
for (key, value) in fiber {
if key == "value" {
self.numberOfFiberGrams.text = "\(value.stringValue)"
} else {
self.numberOfFiberGrams.text = "Fail"
}
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
그리고 여기가
이
은 'configureLabels을()'이 포함 된 코드 방법 'configureLabels()'가 호출하는 함수를 포함하는 코드 :func ndbnoRequest(ndbno: String, apiKey: String, completionHandler: @escaping (_ results: JSON?) -> Void) {
Alamofire.request("https://api.nal.usda.gov/ndb/V2/reports?ndbno=\(ndbno)&type=f&format=json&api_key=\(apiKey)", method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
completionHandler(json)
print("successful ndbno request")
case .failure(let error):
completionHandler(nil)
print(error)
}
}
}
무슨 일이 있었습니까? 앱이 종료됩니까? 아무 일도하지? –
요청에서 검색 한 데이터로 레이블이 업데이트되지 않습니다. –