Array declared in class but the value can't be accessed by more than one method in that class?

The following code gives me an "error: <EXPR>:1:1: error: use of unresolved identifier 'sampleArray'." Shouldn't I be able to declare an array in a class, add to it in one method and then access it's data in another method in the same class? It keeps giving me this error and it goes against everything I've known about object oriented programming. I must be missing something obvious. Please help me find a solution to this.


class SampleController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var sampleArray= Array<String>()


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

cell.title.text = sampleArray[ndexPath.row] as String //error: <EXPR>:1:1: error: use of unresolved identifier 'sampleArray'

}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

sampleArray.append("Data 1")

sampleArray.append("Data 2")

}

}

That code can't compile; please give us the actual code.

var sampleArray = [String]()

is how to declare that variable. You need a space before the =.

Unfortunately, adding = [String]() did not fix it.


class SampleController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var sampleArray = [String]()


override func viewDidLoad() {

super.viewDidLoad();

}


override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

{

return 10

}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

title = sampleArray[indexPath.row] as String //error: <EXPR>:1:1: error: use of unresolved identifier 'sampleArray'

}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

sampleArray.append("Data 1")

sampleArray.append("Data 2")

}

}

In addition to the space needed between the variable name and equals sign in the line which Jessy mentioned

var sampleArray = Array<String>()


you have another typo in the line causing the error where you lost the i of indexPath

title = sampleArray[indexPath.row] as String //error: <EXPR>:1:1: error: use of unresolved...

Thank you for pointing that out. I fixed the typo but I still get the error.

With the space added (which is still not corrected in the code you edited above), the only error I get with your code in a Xcode 7 beta 3 playground is that your function isn't returning the value it is supposed to (presumably because you haven't written that part yet).

It's corrected. Let's focus on the question at hand which is about the array. Why can't I access array contents in cellForRowAtIndexPath when it is set in didSelectRowAtIndexPath? Doesn't make any sense that I'm getting this error since sampleArray is in scope of both.

I get the same result that LCS did: an error message about a missing return, that's all.


There are a couple of situations where Xcode seems reluctant to "let go" of an error message from an earlier compilation attempt. Maybe that's happened to you. You can try any of the following:


1. Put the text cursor immediately to the right of the "y" in sampleArray and press Esc. This should pop up a completions list, and sampleArray should be the only thing in the list. Press Enter to use the completion.


2. Select and cut the entire line of text from that method. Wait a few seconds to make sure you get a different error message (missing return), then paste the line back in.


3. Clean the build folder: Option + Clean Build Folder from the product menu. (This should get rid of old build transcripts.)

And, not an issue which could be causing sampleArray to not to be recognized, but...


You are also setting the 'title' parameter inherited from UIViewController

(var title: String? // Localized title for use by a parent controller.)

instead of using a local variable.

Is this what you intended?

let title = sampleArray[indexPath.row] as String


Also, there is a runtime issue you haven't hit yet if your code won't compile, but if the table is visible it will probably be asking for the data using tableView:cellForRowAtIndexPath: before tableView:didSelectRowAtIndexPath: is called and you will get a runtime exception because the array is still empty and tableView:numberOfRowsInSection: is saying there are 10 rows.

If you want to fill your array with placeholder data, doing it in viewDidLoad() will make sure it is available for the other methods.

QuinceyMorris those were helpful suggestions. In the above code sampleArray isn't even being recognized inside didSelectRowAtIndexPath so no appending to the array is even taking place. There's got to be something obvious missing.

Array declared in class but the value can't be accessed by more than one method in that class?
 
 
Q