title: Parsing units and amounts from text in swift
body: <p>I was wondering what the best approach would be to separate multiple items from a string in swift. I'm hoping to separate a unit and an amount from a string and then use those values to create an object of my ingredient class. </p>
  
  <p>For example: </p>
  
  <pre><code>var string = "4 cups sugar"
  </code></pre>
  
  <p>I would need to grab the 4 (amount) and convert it to an int and then grab the unit (cups) </p>
  
  <pre><code>var amount = 4  
  var unit = "cups"
  </code></pre>
  
  <p>Another example: </p>
  
  <pre><code>"3 large eggs"
  </code></pre>
  
  <p>In this case I would want to pull out the 3 (amount) and the unit would be empty.</p>
  
  <pre><code>var amount = 3
  var unit = ""
  </code></pre>
  
  <p>Then I would create my object using the unit and amount values.</p>
  
  <p>I'm still a novice at swift, and more-so with string manipulation so I'm not entirely sure how to approach this, any help in the right direction would be great. </p>
  
  <p>I am working with an ingredient class that is structured as:</p>
  
  <pre><code>class IngredientModel {  
  var amount = 0
  var unit = ""
  
  init(amount : Int, unit : String) {
      self.amount = amount
      self.unit = unit
  }
  </code></pre>