0

I am trying to use the array of strings that is named "theList" in another class in my project. I understand that the variable is declared within a method and therefore not of global scope. But how can I fix that? I have tried a few things and nothing is working. What is the best way to accomplish this?

EDIT: All I want to do is set "theList" equal to "Body" [![enter image description here][1]][1]

I hope this clears it up a little bit, and I can select an answer. Thanks to everyone!

Danny06191
  • 133
  • 1
  • 9
  • Possible duplicate of [Access variable in different class - Swift](http://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift) – Beraliv Oct 24 '15 at 23:09

4 Answers4

1

I think if you want to access that list outside of the function you should simply make it a variable of the class.

class TaskManager: NSObject {
    //Sets up array of Tasks
    var tasks = [task]()
    var theList = [String]()

    //Add Task Function
    func addTask(serial: String){
        tasks.append(task(serial: serial))

    theList = tasks.map({ (task) -> String in
        return task.serial
    })
  }
}
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
1

Your best way to do this is to look at the scope of your class and the declaration. Define your list in a place that has scope everywhere you want to use it, or make it global. That's how I do this kind of task. Sometimes add it to a higher level class declaration, and sometimes I make it global to, say, the entire program. Depends on the task and my outlook at the time of coding.

K17
  • 697
  • 3
  • 8
  • 26
  • Thanks. How would you do that? – Danny06191 Oct 25 '15 at 00:12
  • Lets say you want a variable to be global. First search via google or bing for an example of how you would make things global in Xcode. Lets say I want to do it for an int type variable. Then, I will put int myInt = 0; somewhere outside of main. If your class variable is dependent on the definition of some other variable, they you have to get them all in the right order in the file. There should be a good example if you search on some phrase like "global variables in Xcode". – K17 Oct 25 '15 at 00:18
0

Let you function return the results of adding the task then:

class TaskManager: NSObject {
    // declare your variables

    func addTask(serial: String) -> [String] {
        // put your realisation
    }
}

or make your variable with serials available publicly:

class TaskManager: NSObject {
    var tasks = [Task]()
    public var serials = [String]()

    func addTask(serial: String) {
        // put your realisation
        // save the results to serials
    }
} 

and then access it via the instance.

Beraliv
  • 518
  • 7
  • 20
0

Adding theList Variable above class made it global. Then I had to remove let from the addTask function.

//Add Task Function
        func addTask(serial: String){
            tasks.append(task(serial: serial))

       let theList = tasks.map({ (task) -> String in
            return task.serial
        }).joinWithSeparator("\n")

Became

//Add Task Function
        func addTask(serial: String){
            tasks.append(task(serial: serial))

       theList = tasks.map({ (task) -> String in
            return task.serial
        }).joinWithSeparator("\n")

The final code is as follows.

import UIKit

var theList : String = String()
var taskMgr: TaskManager = TaskManager()

struct task {
    var serial = "Un-Named"

}
public class TaskManager: NSObject {
    //Sets up array of Tasks
    var tasks = [task]()

    //Add Task Function
    func addTask(serial: String){
        tasks.append(task(serial: serial))

   theList = tasks.map({ (task) -> String in
        return task.serial
    }).joinWithSeparator("\n")

    do {
    //try tasksString.writeToFile(pathToFile, atomically: true, encoding: NSUTF8StringEncoding)
    print(theList)

    }
  }
}

I selected the answer. Thank you to all who helped cure my tired eyes.

Danny06191
  • 133
  • 1
  • 9
  • I see you put theList and taskMgr in a global scope. Perhaps it was easier to make addTask a **class** func and theList a **static** var, because that way you never need an instance of TaskManager and you could access the list simply with TaskManager.theList. – Lucas van Dongen Oct 26 '15 at 00:32