, но мне не удалось достичь того, что мне нужно.
Просто я хочу сохранить ответ от API запросить некоторую переменную
, чтобы я мог свободно ее использовать.
Вот код:
Код: Выделить всё
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var responseLabel: UILabel!
let apiUrlStr = "http://jsonplaceholder.typicode.com/posts"
var resData = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
request(.GET, apiUrlStr, encoding: .JSON).responseJSON { (_, _, JSON, _) in
let arr = JSON as NSArray
let user = arr[0] as NSDictionary
self.responseLabel.text = user.objectForKey("title") as? String
self.resData = JSON as NSArray //i cant get this from outside
}
println(resData) //prints nothing but empty parentheses while inside the request it prints the data right
}
}
Код: Выделить всё
class api{
func req(url: String, params: NSDictionary){
//method goes here
}
func res() -> NSDictionary{
//method goes here
var response = NSDictionary()
return response
}
}
Код: Выделить всё
let params = ["aaa","bbb"]
let api = api()
api.req(apiUrlStr, params)
let res = api.res()
Вот вот некоторые из сообщений и сайтов, за которыми я безуспешно пытался следить.
proper-way-to-pull-json-api-resoponse
link2
все приводят к одному и тому же результату только изнутри метода.
Подробнее здесь: https://stackoverflow.com/questions/282 ... ces-in-ios
Мобильная версия