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!

1

4 Answers 4

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
    })
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I want to access it outside of the class. I am trying to print theList in another file and view
You're trying to save it and load it in another class later?
Yes. I have another class that is going to email the contents of "theList". But in that class it is telling the theList is unresolved.
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.

2 Comments

Thanks. How would you do that?
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".
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.

6 Comments

I want to print theList in another swift file.
@user2913654 then try public class TaskManager
I did try that it still says "Use of unresolved identifier 'theList'" when i call on it
@user2913654 See that post, maybe it'll help you stackoverflow.com/questions/24333142/…
That didn't really help me. Kind of confused me more lol I will give you a better example.
|
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.