如何在两个viewcontrollers之间的swift应用程序中传递json数据

[英]How to pass json data in swift app between two viewcontrollers


I need help with passing json data.I have json data in this array

我需要帮助传递json数据。我在这个数组中有json数据

var TableData:Array< String > = Array < String >()

In this Array I have Name,Address,Latitude, Longitude

在这个数组中我有名字,地址,纬度,经度

I show Name and Address in a tableView , but I would like to create annotation in different viewController with Latitude and Longitude depending on which cell user taps(Name,Adress,latitude,Longitude shoulld be equal) , so I am asking you if there is some good tutorial in swift , or if you have just some advice. Thank you.

我在tableView中显示名称和地址,但我想在不同的viewController中创建具有纬度和经度的注释,具体取决于哪个单元格用户点击(名称,地址,纬度,经度等于),所以我问你是否有swift中的一些很好的教程,或者如果你有一些建议。谢谢。

3 个解决方案

#1


0  

I would do something like this:

我会做这样的事情:

let newController = self.storyboard?.instantiateViewControllerWithIdentifier("newControllerIdentifier") as! NewControllerClassName
newController.data = array
self.navigationController?.pushViewController(newController, animated: true)

It also appears you are using a array of string type that are comma separated. I would rather create a variable like below

您还可以使用以逗号分隔的字符串类型数组。我宁愿创建一个如下变量

var jsonArray:[[String:String]]?

#2


1  

There are many different ways to pass data from one swift file to another. In the case that there is a rootViewController and a button is clicked to open a new ViewController. Having previously defined an array in the new ViewController, the json parsed array can be passed along using the prepareForSegue method. Another popular way to pass information between different swift files would be using the AppDelegate. Here you can create instances of different swift classes using a method known as instantiateViewControllerWithIdentifier("identifier"). This can be done by creating a storyboard variable then calling this method by doing storyboard.instantiateViewControllerWithIdentifier("identifier").

有许多不同的方法可以将数据从一个swift文件传递到另一个swift文件。如果有一个rootViewController,则单击一个按钮以打开一个新的ViewController。之前在新的ViewController中定义了一个数组,可以使用prepareForSegue方法传递json解析数组。在不同的swift文件之间传递信息的另一种流行方式是使用AppDelegate。在这里,您可以使用称为instantiateViewControllerWithIdentifier(“identifier”)的方法创建不同swift类的实例。这可以通过创建一个storyboard变量然后通过storyboard.instantiateViewControllerWithIdentifier(“identifier”)调用此方法来完成。

let newvc = self.storyboard?.instantiateViewControllerWithIdentifier("newvcIdentifier") as! (UIViewController extended class)
newvc.data = TableData
self.navigationController?.pushViewController(newController, animated: true)

where newvc has a variable declared as follows:

其中newvc有一个声明如下的变量:

var data: Array <String>!

Another method that can be used is having shared data among all of the classes using a singleton. A singleton can be created very simply in swift3, take a look here for details.

可以使用的另一种方法是使用单例在所有类之间共享数据。可以在swift3中非常简单地创建单例,详细了解这里。

class JSONData {
    var json: Array <String>
    static let sharedInstance = JSONData()
}

Before segueing to the next vc, you should store the data in the sharedInstance class. This should be done by overriding the prepare method. Documentation on this method can be found here

在转到下一个vc之前,您应该将数据存储在sharedInstance类中。这应该通过覆盖prepare方法来完成。有关此方法的文档可在此处找到

sharedInstance.json = self.json

Once the data is set in the shared instance, after the new view controller is loaded, this data can be accessed through sharedInstance.json.

在共享实例中设置数据后,在加载新视图控制器之后,可以通过sharedInstance.json访问此数据。

Hope this works for you and just comment if you have any other questions!

希望这对您有用,如果您有任何其他问题,请发表评论!

#3


0  

so I am asking you if there is some good tutorial in swift

所以我问你是否在swift中有一些很好的教程

http://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/

He covers clean code and best practices. Better read this before implementing anything.

他介绍了清洁代码和最佳实践。在实施任何内容之前,最好先阅

or if you have just some advice

或者如果你有一些建议

Your code needs to be clean. Passing latitude as String it's going to create you a problem later (conversions, code understanding, or even bugs). Make a new account here and watch uncle Bob. His lessons are valuable. matteomanferdini above follow uncle bob practices. https://www.safaribooksonline.com/library/view/clean-code/9780134661742/

你的代码需要干净。将纬度作为String传递它会在以后创建一个问题(转换,代码理解,甚至错误)。在这里开个新账户,看看叔叔鲍勃。他的课程很有价值。 matteomanferdini上面跟随叔叔鲍勃的做法。 https://www.safaribooksonline.com/library/view/clean-code/9780134661742/


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/01/20/9f0702b820743989dfd46ccb0b4812b8.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号