Accessing Dictionary outside its method

Hi, I have a view controller that includes a method to create a dictionary. I can access the items in the dictionary inside the method. But I cannot access the items outside the method even within the same view controller. I want to access the items in the dictionary to create cells in a tableview cell in another view controller.

I am missing something fundamental about how to do this and I have not been able to figure it out with numerous google searches.

Any help would be greatly appreciated.


Yes, what you’re talking about is variable scope.
When you create a variable (your dictionary) inside a method. It can only be accessed in there.

Try actually declaring the dictionary in the class (like in class ViewController)

But in your method actually populate it

Hi Mike, I had declared the dictionary in a separate class. However, it wasn’t a ViewController class. I just made it a subclass of UIViewController but I still can’t access it outside the method.

Ok, I am now able to read the dictionary values. They don’t show top the first time the button is tapped bu they show uptake second and subsequent times. Not sure why that is.

must be because your data is not called on init (viewdidload)?

Thanks Francis, so what do I have to call in viewdidload and which controller’s viewdid load?

In one controller the array of data is generated when I tap on a button, it then segues to another view controller to display the data.

why cant you just move the generation of data to the other view controller alongside displaying the data?

Hi Francis, I was thinking the same thing.

However, when I moved the generation of the data to the other view controller alongside displaying the data and then called the function from viewdidload I still get a blank tableviewcell the first time.

I then tried the override function viewwillappear with no difference.

did you try printing the data array after you called it? so you can check if the array really did populate

Yes, the first time it prints it is empty. The second time I run it and it prints it is populated.

can you post your code on how you populate it?. there must be something wrong with the order you populated your array, try switching some code around

Hi Francis, sorry I was tied up for a few days on something else.

Attached is a link to sample code for one of the view controllers.

I have tried switching code around and what I have determined is that the sequence seems to go to the override func for the collection view before it populates the array. The array is called up as a get function in the override func viewdidload.

When I step through the code it calls the get function, appears to ge to the fire cloud database and retrieve the information but as part of that step it seems to go the override func for the collection view. Since the array hasn’t been populated it displays a blank view.

But when I step through the code a second time the array gets populated and the collection view gets displayed.

I should also note that sometimes I have to step through the code more than a second time to display the data.

Anyone have any ideas?


I think the best hack would be to put another getEngineReport() at your viewDidLoad so it loads the the data first, then calls viewWillAppear to load again

what i see is that the main issue is in the


because the count is based on data, which you do not have on your first run

Ok, that might explain it, but I already tried putting another getEngineReport() in viewDidLoad and that didn’t work. Also, sometimes it takes two or three refreshes before I see the data.

How should I have coded this? Is there a better way to create the cell?

Thanks for the help

its because it may take some time to load the data so it doesn’t show immediately after clicking

i think it would be better to create a “loading screen” where you get the data and save it globally for use, then you show the table once its loaded

or maybe just simply open it with a blank table, put a load data button, press it (maybe put on a loading animation for a few secs, then populate the tableview

Thanks, I was thinking of a loading screen as well. But what I find strange is it seems to run through the for loop without actually getting the data. I put a print database command just inside the four loop and it prints an empty database 28 times and then calls up the collection view which obviously shows an empty cell.

What would cause it to loop through the database 28 times and not actually create the data?

thats quite weird if it got the “length” but not the data

how do you initialize your “EngMainStatus” anyway? i see you are using it but i dont it see initialized anyway, like where did the data come from?

I initialize it in a separate structure.

After many hours of google searches I found the following code works when placed in the numberOfItemsInSection collection view function.


I am not sure what this does but it works.

Thanks for your help Francis