title: iOS9 - Saving a string to Core Data
body: <p>I know this question has been asked and answered many times but no matter how many solutions I try, I can't get it to work.  </p>
  
  <p>So, my application will store user information about the current user that is logged in. I want to store it as a simple User object in Core Data. </p>
  
  <p>I've created a new entity Called <code>UserData</code> with following attributes: </p>
  
  <pre><code>name
  username
  profilePicture
  </code></pre>
  
  <p>When the user logs in, these values are fetched from a server and shall be stored locally in Core Data. </p>
  
  <p>I have generated <code>NSManagedObject</code> subclasses for my entity and ended up with the following two classes</p>
  
  <pre><code>//  UserData.swift
  //  Created by xxx on 2015-10-14.
  
  import Foundation
  import CoreData
  
  class UserData: NSManagedObject {
  
  // Insert code here to add functionality to your managed object subclass
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>//  UserData+CoreDataProperties.swift
  //  Created by xxx on 2015-10-14.
  
  //  Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
  //  to delete and recreate this implementation file for your updated model.
  
  import Foundation
  import CoreData
  
  extension UserData {
  
    @NSManaged var profilePicture: NSData?
    @NSManaged var username: String?
    @NSManaged var name: String?
  
  }
  </code></pre>
  
  <p>There are two functions called whenever the username should be saved or obtained from Core Data</p>
  
  <pre><code>func saveUsername(username: String) {
  
      let appDelegate =
      UIApplication.sharedApplication().delegate as! AppDelegate
  
      let managedContext = appDelegate.managedObjectContext
  
      let entity =  NSEntityDescription.entityForName("UserData",
          inManagedObjectContext:managedContext)
  
      let user = UserData(entity: entity!,
          insertIntoManagedObjectContext: managedContext)
  
      user.setValue(username, forKey: "name")
  
      do {
          try managedContext.save()
      } catch let error as NSError  {
          print("Could not save \(error), \(error.userInfo)")
      }
  }
  
  func getUsername() -&gt; String{
      let appDelegate =
      UIApplication.sharedApplication().delegate as! AppDelegate
  
      let managedContext = appDelegate.managedObjectContext
  
      let fetchRequest = NSFetchRequest(entityName: "UserData")
  
      var users = [UserData]()
  
      do {
          let results =
          try managedContext.executeFetchRequest(fetchRequest)
          users = results as! [UserData]
      } catch let error as NSError {
          print("Could not fetch \(error), \(error.userInfo)")
      }
      return users[0].name!
  }
  </code></pre>
  
  <p>This is my first try to use core data and it seems like I'm missing something. Can anyone see what could be wrong here? I'm not sure if the data is saved correctly and the <code>results</code> from the <code>fetchRequest</code> is an empty array.</p>