How to Pass Array Object between view controllers by reference - ios

I am trying to pass an Array Object between 2 view controllers. But I want the Array to be passed as a Reference and not copied. I know if I was using a function locally, I can use "inout" and "&" based on the examples I found.
But I what I want to do is slightly different. I want to assign the Array in VC1 directly to the Array object in VC2 as a reference. Here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "vc2" {
let vc2 = segue.destinationViewController as! ProfileVC
vc2.data = &self.data //I know this line doesn't work, how would I go about it?
}
}

Swift array are struct, so they will get copied.
The simplest way would be to use an object instead of a struct. So I would use a NSMutableArray instead.
With that you will have all the power of mutability at your finger tips ;-)

Related

Passing Data Between two ViewControllers in MVVM

I recently started exploring MVVM and stuck at passing data between 2 view controllers.I have an array of items in my viewModel1 of ViewController1 and I want to pass that array to viewcontroller2. I am able to achieve it by getting the whole array from viewModel1 like below but I am not sure if this is the right way to do it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Constants.kShowDetailSegue {
let destinationViewController = segue.destination as! ListViewController
destinationViewController.products = viewModel1.getFavouriteProducts()
//the getFavouriteProducts gets data from coredata.
}
}
I think you have done it the right way, but alternatively you can use closures. Which are the preferred, method of communication within iOS. You will take care of memory leaks wherever necessary by using
weak
OR
unowned

How to pass data from ViewControllers and show them into tableviewCell?

I want to pass data from my ViewController to TableViewCell, How can I do that. How to pass data from one ViewController and show them in TableViewCell.
The question is a bit too vague, but I will try. If you are using a segue from another viewController and the data is, for example, an array of Strings i.e in your first viewController:
var arrayOfStrings = ["StringRow1", "StringRow2"] // etc
and you want to be able to access this array when you enter a new viewController and use it for your tableView then you can use prepareForSegue
First, in your viewController with the tableView have a variable ready for the array:
var arrayOfStrings = [String]()
Then you can pass the data over like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueToTableView" {
if let vc = segue.destination as? YourTableViewController
{
vc.arrayOfStrings = self.arrayOfStrings
}
}
}
Now when you arrive in your tableView viewController, your arrayOfStrings data will be passed.
This is a basic simple example as you did not provide much context.
Creat variable in your cell class and in cell for row just assign that value to variable and use in your cell class.

how can I pass a swift array by reference between viewControllers in prepare(for segue:?

I know that swift array structs are copied by value and not reference. Is there a simple way that I can cast or otherwise pass the array by reference in my prepare(for segue:... method so that I may pass a reference to the array from viewController A to viewController B?
I have seen some answers that refer to passing an array to a function but I simply want to pass it to other viewControllers
Create wrapper class for your array and then pass that class reference wherever you want:
class ArrayWrapper {
let array: [YourType]
init(array: [YourType]) {
self.array = array
}
}
Yes you can achieve it by passing the values from viewController A to viewController B. All you need to do is, take an array (arrB) in ViewController B .
let VB = self.storyboard?.instantiateViewController(withIdentifier: "viewControllerB") as! viewControllerB
vc.arrA = arrA
self.navigationController?.pushViewController(vc, animated: true)
This will not pass reference but the values of area to arrB.
If u want to transfer array from one controller to another controller using segue create array in your first controller like this
var dataSource : [ANY MODEL CLASS] = []
use this method to transfer your data using segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "YOURSEGUE") {
let vc = segue.destination as! YOUCONTROLLER
vc.myArray = self.dataSource
}
in your second controller create same type array
var myArray : [ANY MODEL CLASS] = []
so now when you moved to another controller using this "YOURSEGUE" segue your array will have data
create a global array like this
var myGlobalArray = [Int]()
class viewControllerA : UIViewController {
}
now you can use it everywhere. but this is not a good thing
If you cast the array into NSArray it should do the work.

How to parse json when collectionView is tap

Hello I'm just wondering if there is another way to parse json data when a collection view is click and pass it to another view. This is the API im using. This is how I do it at the moment. This is how my class looks. First I create a closure. Then I created my hero class. Then I have my json parse function.
This is how my collectionView didSelectItemAt func looks. This is where im trying to put my json parse function, or is it better to put it in the hero class. Im new to this so I don't really know.
Not sure if I explained it well. So I'll include my viewcontroller code if needed. Would appreciate any help :)
You should create variable for another viewcontroller and pass the data to that variable by using didselect method
After perform Segue you need to do following code to pass the Hero object to HeroDetail Controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Pass the selected object to the new view controller.
if segue.identifier == "HeroDetails" {
let heroDetailVC = segue.destinationViewController as! HeroDetailVC
heroDetailVC.hero = sender as! Hero
}
}
Make sure you have created Variable for Hero Object in HeroDetail Controller.
This will help you to pass the data over multiple controller using Segue Method.

How do I access an array from one viewcontroller in another viewcontroller

I am very new to the swift language and am trying to make an app that would do some basic calculations. The problem I am running into is that I want to use the values from an array that is declared in one viewcontroller in another viewcontroller to do calculations.
I declare the array in the first viewcontroller like this:
var array2 = [Double]()
And then I have no idea how to access this in the second view controller. I have tried looking at previous questions as well as online tutorials and have had no success.
Any help is greatly appreciated. Thank you in advance.
in VC2 create global var array2 = [Double]()
add following override func to body of VC1
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
Overall, what you are doing is telling VC1 to copy VC1's array2 to VC2's array2, before the segue happens.
As your app becomes more complex, if your VC1 has more than 1 segue...meaning can go to multiple different VC's...you'll need to change your prepareForSegue so that it takes into account for this.
One way to do this is to change it to
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier you give to that particular segue in identityInspector" {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
}

Resources