ios - 使用 Button 快速传递数据

标签 ios json swift

我尝试将数据从我的 ViewController 传递到 TableViewPlace.swift 使用按钮。当我选择按钮返回时不是什么东西

viViewController

class ViewController: UIViewController {

    struct Country{
        var id :Int
        init(id:Int){

            self.id = id
        }
}
var CountrySelected = [Country]()
var countryArry = NSArray()



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func airAction(sender: AnyObject) {
}


@IBAction func viewPlaceAction(sender: AnyObject) {


    getParsePlaceView()
     // json viewPlace


    performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}

@IBAction func tourAction(sender: AnyObject) {
}
/// Open the page
    // parse json
func getParsePlaceView(){

    let url = NSURL(string: "http://jsonplaceholder.typicode.com/posts")
    NSURLSession.sharedSession().dataTaskWithURL(url!){ [unowned self] (data , repsonse , error) in
        if error != nil {
            print(error!)
        } else {

            do {
                let posts = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]

                for post in posts {

                    if let id = post["userId"] as? Int{

                     // print(id)

                        let sets = Country(id: id)
                        self.CountrySelected.append(sets)
                    }


                }
                self.countryArry = posts
                print(self.countryArry)// return data and currect

            } catch let error as NSError {
                print(error)
            }
        }
        }.resume()

   print(countryArry) // return nil why??
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // get a reference to the second view controller
    if segue.identifier == "viewPlaceSegu" {
        if let secondViewController = segue.destinationViewController as? TableViewPlace {

            // set a variable in the second view controller with the String to pass
            print(countryArry)
            secondViewController.tnt = countryArry
        }

    }
}


}

当我 print(countryArry) 返回 nil 为什么??

有人可以帮助我或给我更好的解决方案吗?

最佳答案

您正在使用此行创建一个 Post 对象:

let countryPlace = Post(userid: post["userId"] as! Int, title: post["title"] as! String)

并且您试图将此 Post 对象传递给您的 tableview 到您的 tnt 变量,这是一个 ViewController 类,这就是为什么您出现第一个错误:

secondViewController.tnt = Country 

然后,如果你想将你的字符串国家添加到你必须做的可变数组中:

if let title = tnt.title {
   Country.append(title)
}

关于ios - 使用 Button 快速传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596848/

相关文章:

python - 将 MySQL blob 内容作为 json 响应发送

objective-c - 如何将文件上传到iOS上我的documents文件夹中存储的服务器?

ios - 后台的 URLSessionDownloadTask 从不触发 handle(_ backgroundTasks : Set<WKRefreshBackgroundTask>) for WKURLSession​Refresh​Background​Task

ios - 在 Swift 中上传 Google Drive 上的电子表格文件

ios - 当你发出网络请求并且它没有带来图像时如何设置默认图像?

swift - 将 CMTime 转换为字符串是错误的值返回

ios - 如何手动删除 Cocoalumberjack 日志

php - 在 iPhone 上获取数据

arrays - 如何在多个数组的索引处保存多个元素?

ios - 使用 UIKit Dynamics 动画自动布局支持 View