1

I have created a PickerView in a SegmentControl, I am having two separate array FirstArray has StringData, Second Array has NSNumber data. I need to return both the array in order to return as StringDataand Number in same row.

For Example: First Array has : Ark,silk,temp etc, Second Array has 23,45,56 etc.

If I return the first Array alone in PickerView it was returning String value, but I need both array to be displayed.I am done with displaying one array in picker view but don't know how to return two arrays. Code:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

   if (pickerView == EventsPickerView) {

   // case EventsPickerView:
        return EventArray.count

   }else {

    switch FabricSegmentControl.selectedSegmentIndex {

    case 0:

        return globalConstants.ClassificationName.count
          //  return globalConstants.ClassificationName.count + globalConstants.ClassificationCount.count

    case 1:

        return globalConstants.ClassificationNameSecond.count





    default:
        return 1
    }
}
  //  return 1
}

 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

 if  (pickerView == EventsPickerView){

   // case EventsPickerView:
        return EventArray[row]

 }else {

    switch FabricSegmentControl.selectedSegmentIndex {

    case 0:

        return globalConstants.ClassificationName[row] as? String




    case 1:

        return globalConstants.ClassificationNameSecond[row] as? String



    default:
        return "error occured"
    }

}

   // return "error occured"


}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if (pickerView == EventsPickerView){

        eventTextField.text = EventArray[row]
    }else {

        switch FabricSegmentControl.selectedSegmentIndex {

        case 0:



                TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String




        case 1:

              TypeOfSilkTextField.text = globalConstants.ClassificationNameSecond[row] as? String


        default:
            break
        }
    }

These are my array Names

First Array :globalConstants.ClassificationName.count

SecondArray name :globalConstants.ClassificationCount.count

2
  • If the data in the two arrays belong together why don’t you create a struct holding a string and a number or use a tuple and then have a single array of struct’s (or tuples)? Commented Jul 20, 2018 at 13:45
  • I will try this and update. Commented Jul 20, 2018 at 14:03

2 Answers 2

3

You are returing NSNumber as String. That's why a nil value is returning. Try converting NSNumber into String.

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

Comments

2

As the 2 arrays are equal so return one of them here

return globalConstants.ClassificationName.count

//

In titleForRow

 return globalConstants.ClassificationName[row] as? String ?? "" + ( globalConstants.ClassificationCount[row] as? String ?? "" )

//

In didSelectRow

TypeOfSilkTextField.text = globalConstants.ClassificationName[row] as? String ?? ""  + ( globalConstants.ClassificationCount[row] as? String ?? "" )

but it's better creating a struct to hold them as one unit

//

What makes sense to declare it like

 static let ClassificationName = ["value","value2"]

which will be inferred to array of strings [String]--- to return ClassificationName[row]

and

 static let ClassificationCount = [25,26]

which will be inferred to array of ints [Int]--- to return "\(ClassificationCount[row])"

Or be

 static let ClassificationCount = ["25","26"]

which will be inferred to array of Strings [String]--- to return ClassificationCount[row]

1 Comment

@V_rohit according to your question you attached the arrays as optionals Any

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.