0

I can currently get rid of repeating objects in an array, but I want to call that array in the indexPath.row method, but I get the error that the variable is not defined since it is declared in a function. I tried instantiating as a nil globally, but that does not get rid of the repeating objects.

Var myVar = [<the T type>]() uniq(<some source>, uniqueTitle: &myVar) func uniq<S : SequenceType, T : Hashable where S.Generator.Element == T>(source: S) -> [T] { var buffer = [T]() var added = Set<T>() for elem in source { if !added.contains(elem) { buffer.append(elem) added.insert(elem) } } return buffer } //titleatcell is an array with repeating objects. titleatcell.append(title!) var uniqueTitle = uniq(titleatcell) uniqueTitle.append(title!) print(uniqueTitle) //return count of objects in array } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) //Error: uniqueTitle is not declared. cell.textLabel?.text = uniqueTitle[indexPath.row] return cell } 
3
  • Try this cell.textLabel?.text = uniq(titleatcell)[indexPath.row] Commented Oct 19, 2015 at 4:33
  • uniq is not declared Commented Oct 19, 2015 at 4:52
  • You seem to have declared uniq as a nested function. Is that really what you wanted to do? If not, bring the declaration of uniq to your class scope, like right before tableView(t:cellForRowAtIndexPath:). Commented Oct 19, 2015 at 4:55

3 Answers 3

1

How do I access a variable declared in a function?

There is no way you can do this. A variable declared locally inside a function is not known to outside world.

What you are looking for is pretty straightforward. Create a function that accepts an array and returns back the massaged array based on your business rules (after removing duplicates).

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I was trying to figure that out, but I kept on getting the original array back. after the var uniqueTitle = uniq(titleatcell), could I say titleatcell == uniqueTitle?
1

You can either pass uniqueTitle as an inout parameter to uniq, so you can set it within the uniq function. Or you can return uniqueTitle from uniq and then pass it to your second function.

For example:

Your function signature for uniq may be

func uniq<S : SequenceType, T : Hashable where S.Generator.Element == T>(source: S, inout uniqueTitle: [T]) -> [T] 

And then you can pass in a globally defined variable like:

Var myVar = [<the T type>]() uniq(<some source>, uniqueTitle: &myVar) 

Now you can access myVar anywhere inside the same scope.

5 Comments

How would that look programmatically?
I get an error when declaring it globally like this I will update my answer. I don't think I am placing this in the correct spot
The angled brackets <> need to be replaced. Like you need to replace <some source> and <the T type> with your source object and the type T you want to use with uniq
I'm still confused what you mean by replace <some source> and <the T type> with your source object and the type T you want to use with uniq
@xxtmanxx show me how you were calling uniq in the first place and I'll try and show you what I mean.
0

Seems like your messing with a UITableView and probably UITableViewDelegate all within a UIViewController. You could just make it a variable of your UIViewController class. Then it would be accessible from any function within the class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.