title: Get an Array of days from first day of the month till today Swift
body: <p>I am new in iOS and programming and I need somehow to get an array from the first day of current month till today. And one array for past 3 months till today, but I have no idea how to do that, please any help or ideas?</p>
  
  <p>I checked about this to get the first day of month:</p>
  
  <pre><code>extension Date {
  func startOfMonth() -&gt; Date? {
      let comp: DateComponents = Calendar.current.dateComponents([.year, .month, .hour], from: Calendar.current.startOfDay(for: self))
      return Calendar.current.date(from: comp)!
  }
  </code></pre>
  
  <p>but it works only in a ViewController, what to do if do that in other part of my project? and also I have no idea how to iterate the array to get all the days between first day and today...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I made something like this, but it gives ma an infinite loops.. what am I doing wrong?</p>
  
  <pre><code>    func weatherDatesFromCurrentDayMonth() -&gt; [Any] {
      var date = Date()
      let currentCalendar = Calendar.current
      var dateComponents = DateComponents()
      dateComponents.month = -1
      //  dateComponents.day = 1
      let endingDate = Calendar.current.date(byAdding: dateComponents, to: date)
      print("\(endingDate!)")
        var datesArray = Array&lt;Any&gt;()
  
      while date.compare(endingDate!) != ComparisonResult.orderedAscending
      {
          var dateComponents = DateComponents()
          dateComponents.day = 1
          date = Calendar.current.date(byAdding: dateComponents, to: date)!
          datesArray.append(date)
          print("\(datesArray)")
      }
  
      return [datesArray]
  
  }
  </code></pre>