How do I fill an array in a different view controller without opening the view controller?

I am trying to fill an array in a different view controller and different class. What I am trying to do is check to see if a string is in an array in a different view controller. If the array does contain the string, then I want to display a message and if the array does not contain the string, then I want to append the string to the array. I actually want to send more data with the string. I am thinking that I need to use an array of tuples. I want a string for the name and a cost and monthly payment of the string. I want all of this information to be attached to the string. I am sending it to a table view in the other view controller. I only want to display the string in the table view. Then when the row of the string is clicked, I want it to take me to a different view controller and display the rest of the information attached to the clicked string in a table view. How would I do that? I also need to save the information so when I go to different table views, the information is still there. Can anyone provide me with examples of what to do?

Accepted Reply

In addition to this approach with delegation, you could use a different approach.


You want to use an array that is in another VC, without loading it.

So, why don't you define the array outside of class as

var stringArray: [String]?


it could be initialized and updated in VC2, but accessible in VC1 as well (testing non nil required):

if let validStringArray = stringArray {
     // use validStringArray as needed in VC1
}

Replies

Please edit your text to make it more readable. And ask precise question of what you cannot achieve, not the full story of your app, there are too many questions here.


I am trying to fill an array in a different view controller and different class.

What I am trying to do is check to see if a string is in an array in a different view controller. If the array does contain the string, then I want to display a message and if the array does not contain the string, then I want to append the string to the array.


I actually want to send more data with the string.

I am thinking that I need to use an array of tuples.

- I want a string for the name and a cost and monthly payment of the string.

- I want all of this information to be attached to the string.


I am sending it to a table view in the other view controller. I only want to display the string in the table view. Then when the row of the string is clicked, I want it to take me to a different view controller and display the rest of the information attached to the clicked string in a table view.


How would I do that? I also need to save the information so when I go to different table views, the information is still there. Can anyone provide me with examples of what to do?


So, I will answer the first question

I am trying to fill an array in a different view controller and different class.

What I am trying to do is check to see if a string is in an array in a different view controller. If the array does contain the string, then I want to display a message and if the array does not contain the string, then I want to append the string to the array.


There is a first controller VC1 and you want to use data from a second controller VC2.

You link the class2 to VC2, but that is not necessary: class2 could be defined out of VC2.


VC2 must be loaded, even though it may not be visible, but it must exist.

The best here is to use delegation.


See this thread where I explained the pattern (it is a very long thread, look at the beginning only). https://forums.developer.apple.com/thread/111569


Tell if you don't understand something.

In addition to this approach with delegation, you could use a different approach.


You want to use an array that is in another VC, without loading it.

So, why don't you define the array outside of class as

var stringArray: [String]?


it could be initialized and updated in VC2, but accessible in VC1 as well (testing non nil required):

if let validStringArray = stringArray {
     // use validStringArray as needed in VC1
}