ios - 选择器 View 未更新

标签 ios json swift

由于一些问题,我正在尝试使用 Alamofire,这里我有一个 Alamofire,它获取在线 JSON 文件,获取四个结果,将它们放在选择器 View 上,并刷新以便它们可以显示。此处的代码完美运行:

 Alamofire.request(.GET, "https://example.com/json.json", parameters: ["foo": "bar"])
            .responseJSON { response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    self.mypickerview.delegate = self
                    self.mypickerview.dataSource = self
                    self.pickerData = [JSON["One"] as! String, JSON["Two"] as! String, JSON["Three"] as! String, JSON["Four"] as! String]

                    self.mypickerview.delegate = self;

                }




        }

但问题是,使用这段代码,虽然它设法成功获取数据,但它没有显示在选择器 View 中:

// Setup the session to make REST GET call.  Notice the URL is https NOT http!!
let postEndpoint: String = "https://example.com/json.json"
let session = NSURLSession.sharedSession()
let url = NSURL(string: postEndpoint)!

// Make the POST call and handle it in a completion handler
session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
    // Make sure we get an OK response
    guard let realResponse = response as? NSHTTPURLResponse where
        realResponse.statusCode == 200 else {
            print("Not a 200 response")
            return
    }

    // Read the JSON
    do {
        if let ipString = NSString(data:data!, encoding: NSUTF8StringEncoding) {
            // Print what we got from the call
            print(ipString)

            // Parse the JSON to get the data
            let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            let One = jsonDictionary["One"] as! String
            let Two = jsonDictionary["Two"] as! String
            let Three = jsonDictionary["One"] as! String
            let Four = jsonDictionary["One"] as! String


            self.mypickerview.delegate = self
            self.mypickerview.dataSource = self
            self.pickerData = [One, Two, Three, Four]
            self.mypickerview.delegate = self;



        }
    } catch {
        print("bad things happened")
    }

}).resume()

阻止选择器 View 中的选项显示的第二个代码可能有什么问题?

最佳答案

紧接着

self.pickerData = [One, Two, Three, Four]

如@SaintThread 在评论中提到的,调用以下命令强制重新加载主线程中的数据

self.mypickerview.reloadAllComponents()

关于ios - 选择器 View 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38860814/

相关文章:

android - 在 Android 和 iOS 上保护 HTTP API 调用的最简单和最安全的方法是什么?

iphone - iOS:从 super View 中删除一个 View 会导致另一个 View 被删除?

java - 如何设置日期格式( 2018-04-09 10 :10:10 ) input in swagger JSON

swift - 将 UISwitch 切换为 BarButtonItem

ios - 在 App Purchase 中使用受信任的服务器进行收据验证。为什么我没有从服务器获得最新收据有什么想法吗?

php - 在 PHP 中运行密集的批处理,并避免内存耗尽

javascript - 使用 Angularjs 删除后刷新联系人列表

ios - 没有位置坐标没有互联网与显着位置监控

ios - 读取包含子集合的文档的 Firestore 计费

ios - 如何在 AVAudioSession 中使用 iPhone XS 内置麦克风的立体声(2 声道)?