0

My problem is that I have 4 empty arrays:

var Names:[String] = []
var Amounts:[Int] = []
var Dates:[NSDate] = []
var Images:[UIImage] = []

Inside a class NewPersonViewController.

I also have a function inside NewPersonViewController:

func create(){
   Names.append(nameField.text)
   Amounts.append(amountField.text.toInt()!)
   Dates.append(dateToBePaid.date)
}

I call create() when a button is pressed. The function is just a button action, declared inside NewPersonViewController.

My problem: When I print out the arrays in another class (which is in another file) not including Images, I just get this:

[]
[]
[]

Yup, thats my output. Thank you in advance. Side note: I am new to swift :)

richbrz
  • 92
  • 8
  • Firstly, by convention your array variables should begin with a lower case letter, otherwise people will think they are classes. Secondly, where are you printing the arrays from? How is that other object instance getting a reference to this object instance where you have set the values? – Paulw11 Jun 28 '15 at 22:46
  • let NPVC = NewPersonViewController() println(NPVC.Names) println(NPVC.Amounts) println(NPVC.Dates) @Paulw11 – richbrz Jun 28 '15 at 22:48
  • But how does the object running that code get a reference to the NPVC object? (which, again should be `npvc` by convention)? – Paulw11 Jun 28 '15 at 22:49
  • let NPVC = NewPersonViewController() override func viewDidLoad() { super.viewDidLoad() println(NPVC.Names) println(NPVC.Amounts) println(NPVC.Dates) }@Paulw11 – richbrz Jun 28 '15 at 22:51
  • So, it looks like the NewPersonViewController instance you are referring to isn't the one that you subsequently display (which, presumably is presented by a segue). You are simply creating a new instance (which will have empty arrays) and then printing those empty arrays – Paulw11 Jun 28 '15 at 22:52
  • Can you tell me how to use the instance that has the correct values (in code)? I appreciate the suggestion. @Paulw11 – richbrz Jun 28 '15 at 22:57
  • Are you using a segue to display it? It will be the `destinationViewController` accessible from your segue in `prepareForSegue` – Paulw11 Jun 28 '15 at 23:03

2 Answers2

1

Unless you have just posted some toy code, it seems like there are a lot of problems with your design.

However, the specific problem you may be facing is that, in Swift, Arrays are "value types". Basically it means that if you pass around an Array, Swift is actually making copies every time. So if you edit copy 2 in a different class, it does not affect copy 1 in your original class.

If you just want to get your code working without refactoring to deal with this (and you should really refactor), the easiest thing to do is use NSMutableArray instead of Swift arrays. NSMutableArray is a class, which is a "reference type". You can pass that all around, make changes, and use it from anywhere. Because you are really only passing a "reference", there is only ever one array, and changes you make show up everywhere.

justinpawela
  • 1,968
  • 1
  • 14
  • 18
  • It still doesn't work, even after changing the arrays to NSMutableArray. @user2194039 Can you tell me how to share the array between classes? – richbrz Jun 28 '15 at 23:51
  • @richbrz, you could try to set a global array. But That is really not the best idea. Depending on how you have your project setup you might want to look into passing the array from viewController to viewController when you transition from one to the other. This would be done using "prepareForSegue" method and a segue. – the_pantless_coder Jun 29 '15 at 00:11
  • 2
    @richbrz There really isn't much more I can say right now. Your question doesn't show any of your code from either view controller, nor does it describe how they are displayed or how you are transitioning between them. Like others have said, if you are using segues (or Storyboards), you should use `prepareForSegue:`. Try [reading this](http://stackoverflow.com/a/7865100/2194039). – justinpawela Jun 29 '15 at 00:59
0

I'm relatively new here but if you move the arrays out of the ViewController they will become GLOBAL variables and accessible in any other View or Table Controller. I just tried it and it worked just fine.

For Global variables I have always added a prefix of "gl_" so I know for a fact this is a global variable which means they need to be used with care. In general, global variables are risky and IMHO should be used frugally and consider any risks that come from using them...especially security risks.

In my ViewController.swift file:


import UIKit


var gl_Names:[String] = []
var gl_Amounts:[Int] = []
var gl_Dates:[NSDate] = []
var gl_Images:[UIImage] = []


class ViewController: UIViewController {

    @IBOutlet weak var textInput1: UITextField!

    @IBOutlet weak var textInput2: UITextField!

    @IBOutlet weak var textInput3: UITextField!

etc etc