moving picker data between classes

I have defined a swift file Pick.swift with a class Pick.

I want this class to generate 3 arrays from core data. I then want to use these arrays to define a picker with 3 columns. Then I want to use this picker in a view controller.

I have code that works to define the arrays from core data. But I have the following gaps in my skills.

  1. How do I call the functions in Pick when the app starts to generate the initial arrays?
  2. How to I access the arrays from a view controller for defining the picker ?
  3. I also have view controllers that can modify these arrays by adding and deleting items. How do I get this data back to the pick class to re run the functions so it can update the arrays for the view controllers to access?

.

Create a global (outside any class) instance of your class, then access it's properties and call it's functions. You can generate the initial arrays in the class' initializer.


let thePicker = Pick()     // initializer will run, filling the arrays with initial values

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       self.someLabel.text = thePicker.array1[0]  // assumes array contains strings, show the first string in a label
       thePicker.array1.append("Something New")  // assumes array contains strings, adds another item to the array
       thePicker.saveToSource()  // call your method in Pick that saves changed array(s) back to the core data source
   }

}

When I try this I get the following error in each of my table view controllers Missing argument for parameter 'i' in call


I suspect that I have a problem with the init in the class Pick


init(var i:Int,var j:Int,var k:Int,pickerGrades:Array<String>,pickerSizes:Array<String>,pickerProducts:Array<String>){

self.pickerSizes = pickerSizes

self.pickerGrades = pickerGrades

self.pickerProducts = pickerProducts

self.i = 0

self.j = 0

self.k = 0}


On top of this problem I have not found a way to call the functions in Pick. If I put a function call in init I get an error.

First, a tip about posting code: use the <> button in the toolbar at the top of post editor. You can paste your code, highlight it, then click the button, or click the button, then paste your code in the code area (in Safari, use Edit > Paste and Match Style).


If your initializer has parameters, then you have to give it some parameters when you call it. I'm not sure why you are passing in i, j, and k if you are just setting them to zero. However, if you want just one instance of the data that is shared among all your view controllers, you shouldn't be calling the initializer from each view controller anyway. I was imagining something like this:

class Pick {
    var pickerSizes: Array<String>!
    var pickerGrades: Array<String>!
    var pickerProducts: Array<String>!
    var i: Int = 0
    var j: Int = 0
    var k: Int = 0

    init() {
        self.pickerSizes = self.laodSizes()
        self.pickerGrades = self.loadGrades()
        self.pickerProducts = self.loadProducts()
    }

    func loadSizes() -> Array<String> {
        // your code to load the sizes from core data
    }

    func loadGrades() -> Array<String> {
        // your code to load the grades from core data
    }

    func loadProducts() -> Array<String> {
        // your code to load the products from core data
    }

    func saveAll() {
       // your code to save the arrays to the core data source
    }
    // all your other code for this class
}
let globalPick = Pick()    // an instance is created (initializer runs) when the app starts.


class MyViewController: UIViewController {
   // all your outlets, properties, etc. defined


    override func viewDidLoad() {
       // examples of using globalPick; could be anywhere in your functions for this class
       self.someProperty = globalPick.pickerSizes[2]
       globalPick.pickerGrades.append("New Grade")
       globalPick.k = 23
       globalPick.saveAll()
    }
}


class AnotherViewController: UIViewController {
   // all your outlets, properties, etc. defined

    override func viewDidLoad() {
       // examples of using globalPick; could be anywhere in your functions for this class
       self.someProperty = globalPick.pickerProducts[7]
       globalPick.pickerSizes.append("New Size")
       globalPick.i = 10
       globalPick.saveAll()
    }
}
moving picker data between classes
 
 
Q