0

I am trying to loop through an array of names to get the index of a certain string. I then want to set the index of my UIPicker to the said string.

I have the following code however this causes the app to crash due:

let index = self.nameArray.index(where: {$0 == assetArray[0].Owner })
        scroll_owners.selectRow(index ?? 0, inComponent: 0, animated: true)

When debugging the index is getting a value of index 6176573120 which of course isn't in the range of my UIPicker so causes the crash.

Any ideas on why this may be happening?

Using the suggested answer throws the following error:

unrecognized selector sent to instance 0x101134af0'

There is definitely a match in assetArray[0] with the name that is being passed through.

Doing a bit more investigation trying to run the following line of code alone gives the same error:

scroll_owners.selectRow(0, inComponent: 0, animated: true)

Does this mean I'm missing a delegate method?

Asset Array and Name Array:

var assetArray : [Asset] = []

var nameArray = [String]()

EDIT:

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
}

    self.scroll_owners.delegate = self
    self.scroll_owners.dataSource = self

I've tried to get this working another way - I know this is an ugly way of doing it I'm just trying to see why the accepted swift way isn't working:

            var i : Int = 0
        while (nameArray[i] != name)
        {
            print(nameArray[i])
            i=i+1
        }
        scroll_owners.selectRow(i, inComponent: 0, animated: true)

This section of code crashes and the while loops is never entered due to the index being out of bounds - does this mean the issue could be with nameArray?

12
  • Can you update question with declaration of assetArray class and nameArray Commented Nov 16, 2017 at 8:44
  • and also does it say what selector it doesn't recognise? Commented Nov 16, 2017 at 8:45
  • [UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x154037720 Commented Nov 16, 2017 at 8:46
  • it looks like scroll_owners is not a UIPicker Commented Nov 16, 2017 at 8:49
  • @IBOutlet weak var scroll_owners: UIPickerView! Commented Nov 16, 2017 at 8:50

2 Answers 2

1

i think the problem is that .index doesn't return an IndexPath. But selectRow needs an indexPath as parameter.

.index .selectRow

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

Comments

0

I have managed to solve this error myself in a slightly different way.

In my function which populates the nameArray and the UIPicker I have placed the following code:

                var i : Int = 0
            while (self.nameArray[i] != name)
            {
                print(self.nameArray[i])
                i=i+1
            }
            self.scroll_owners.selectRow(i, inComponent: 0, animated: true)

The reason the code was crashing was due to the fact that the nameArray was not finishing being populated before I was trying to do the comparisons. I know this may not be the accepted swift way of doing this but it works.

The issues were caused due to the function being run on a seperate thread I believe.

Comments

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.