title: Writing predicates to establish an array of counts with a coredata relationships
body: <p>I have two entities in Coredata - Clients and Invoices there is a relationship from Clients to Invoices called clientInvoice. On my first tableViewController I want to list the number of invoices for each client under the client name in the cell.detail label. When a user clicks on the client cell row they are taken to the second tVC which lists the invoices of that client.</p>
  
  <p>To try and get the number of invoices I am using this </p>
  
  <pre><code>fetchrequest
   func FetchCountInvoices() {
      let request = NSFetchRequest&lt;Invoice&gt;(entityName: "Client")
      let clientSort = NSSortDescriptor(key: "name", ascending: true)
      request.sortDescriptors = [clientSort]
      request.predicate = NSPredicate (format: "clientInvoice = %@  &amp;&amp;  year = %@ ", clients!, currentyear)
      do
      {
          let results = try context.count(for: request)
          for number in 0..&lt;(results) {
  
             cInvoices.append(results)
          }
          self.tableView.reloadData()
      }
      catch{
          let nserror = error as NSError
          fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
      }
  }
  </code></pre>
  
  <p>However, all I get is a crash with unexpectedly found nil... Everything I try on the predicate returns this error message. I have looked through all available answers and nothing seems to work so far. I can't believe that something as simple as this is proving to be so beyond me any help would be gratefully appreciated. </p>