title: Using enums case as segment control to show types of data in pickerview
body: <p>I am new in swift and I was thinking a way to populate data ideally with segment control which I did not attempt before.</p>
  
  <p>VC has layout below</p>
  
  <ul>
  <li>CategoryType Segment Control : to control CategoryType)</li>
  <li>CategoryTextField with picker function : keyboard will show list of data from category</li>
  </ul>
  
  <p>Expecting result of selected CategoryType Segment Control to show list of data based on CategoryType on pickerview</p>
  
  <p>This code is in trial and error mode, as I did not have an exact idea on how to execute the result I wish to obtain.</p>
  
  <pre><code> func appendDefaultCategoryTypes() {
          categories = realm.objects(Category.self)
          if categories.count == 0 {
              try! realm.write() {
                  let defaultCategories = [
                      Category(type: .Expense, name: "EXPENSE 1"),
                      Category(type: .Expense, name: "EXPENSE 2"),
                      Category(type: .Income, name: "INCOME 1"),
                      Category(type: .Income, name: "INCOME 2"),
  
                  ]
                  realm.add(defaultCategories)
  
              }
          }
      }
  </code></pre>
  
  <pre><code>
  //MARK: - Transaction Section
  class Transaction : Object {
      //Child of Transaction
      let parentAccount = LinkingObjects(fromType: Account.self, property: "ofTransactions")
  
      @objc dynamic var categoryType : Category?
      @objc dynamic var amount : String = ""
      @objc dynamic var date : String = ""
  
  }
  //MARK: - Transaction Category Section
  
  enum CategoryType : String, CaseIterable {
      case Income = "Income"
      case Expense = "Expense"
  
      static let allValues = [Income, Expense]
  
      init?(id : Int) {
          switch id {
          case 1:
              self = .Income
          case 2:
              self = .Expense
        default:
                   return nil
          }
      }
  
  }
  
  
  
  class Category : Object {
      @objc dynamic var type : String = CategoryType.Income.rawValue
      @objc dynamic var name : String = ""
  
      convenience init(type:CategoryType, name: String) {
          self.init()
          self.type = type.rawValue
          self.name = name
      }
  
  }
  </code></pre>
  
  <pre><code>//VC
      var categories : Results&lt;Category&gt;!
  
      var picker = UIPickerView()
  
      @IBAction func categoryTypeSC(_ sender: UISegmentedControl) {
          guard let selectedCategoryType = CategoryType.(rawValue: sender.selectedSegmentIndex) else {
              fatalError("no corresponding category type for the index selected by segment control")
          }
          switch selectedCategoryType {
          case .income :
              print("Income in SC selected")
          case .expense :
              print("Expense in SC selected")
          }
  
      }
  
      func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
  //        if categorySCoutlet.selectedSegmentIndex == 0 {
  //            return CategoryType.income.count
  //        } else if categorySCoutlet.selectedSegmentIndex == 1 {
  //            return CategoryType.expense.count
  //        }
          return categories.count
      }
  
      func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {
  //        if categorySCoutlet.selectedSegmentIndex == 0 {
  //            return
  //        } else if categorySCoutlet.selectedSegmentIndex == 1 {
  //            return
  //        }
  //        return "None"
          return categories[row].name
      }
  
  </code></pre>