title: How can I set NSFetchedResultsController's section sectionNameKeyPath to be the first letter of a attribute not just the attribute in Swift, NOT ObjC
body: <p>I'm rewriting an old obj-c project in swift that has a tableView with a sectionIndex</p>
  
  <p>I've set a predicate so it only returns objects with the same country attribute</p>
  
  <p>I want to make the section index based on the first letter of the country attribute </p>
  
  <p>in obj-c i created the fetchedResultsController like this </p>
  
  <pre><code>_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                               managedObjectContext:self.managedObjectContext 
        sectionNameKeyPath:@"name.stringGroupByFirstInitial"                                                  cacheName:nil];
  </code></pre>
  
  <p>and i had an extension on NSString </p>
  
  <pre><code>  @implementation NSString (Indexing)
  
  - (NSString *)stringGroupByFirstInitial {
  
      if (!self.length || self.length == 1)
          return self;
  
      return [self substringToIndex:1];
  }
  </code></pre>
  
  <p>this worked fine in conjunction with the two methods </p>
  
  <pre><code>- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView{
  
      return [self.fetchedResultsController sectionIndexTitles];
  }
  - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  
      return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
  }
  </code></pre>
  
  <p>In swift I tried to create a similar extension, with snappier name :)</p>
  
  <pre><code>extension String {
      func firstCharacter()-&gt; String {
          let startIndex = self.startIndex
          let first =  self[...startIndex]
          return String(first)
      }
  }
  </code></pre>
  
  <p>which works fine in a playground returning the string of the first character of any string you call it on.</p>
  
  <p>but using a similar approach creating the fetchedResultsController, in swift, ...</p>
  
  <pre><code>let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
                                                            managedObjectContext: (dataModel?.container.viewContext)!,
                                                            sectionNameKeyPath: "country.firstCharacter()",
                                                            cacheName: nil)
  </code></pre>
  
  <p>...causes an exception. heres the console log</p>
  
  <p>2018-02-23 11:41:20.762650-0800 Splash[5287:459654] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key firstCharacter().'</p>
  
  <p>Any suggestions as the correct way to approach this would be appreciated</p>
  
  <p>This is not a duplicate of the question suggested as this is specifically related to how to achieve this in Swift. I have added the simple correct way to achieve this in Obj -c to the other question</p>