title: Programmatically create entity/table using CoreData and insert values into that entity in Swift 3 for iOS
body: <p>I have been trying to find a way to programmatically create the database entity/table at runtime in Swift 3 for iOS platform.
  Please explain the process in detail. I have followed other links but nothing worked for me and I was unable to understand the process properly so please explain the whole process with working code instead of providing other existing links.</p>
  
  <p><strong>There is one more table which has been added from XCode and I can perform the database related operations (insert row, delete, update) on that table but I am unable to find a way to perform following operations on the table which will be created programmatically at run time.</strong>
  There are following operations that need to be performed.</p>
  
  <p><strong>Add Entity -</strong> From interface, user can add entity name, number of columns, column names then on tapping a button the entity should be created into the database using CoreData. User can add any number of tables from the interface.</p>
  
  <p><strong>Insert Values -</strong> Then user will have interface to insert values into the entity. The name of the entity will be known and the values for a row in the entity will be entered by user. On tapping a button the row should be inserted into the entity/database-table.</p>
  
  <p><strong>Fetch Values -</strong> User will have an option to fetch all the saved values form the entity. To use them anytime when needed.</p>
  
  <p><strong>Update value -</strong> User will be able to update any value present in the table which was created programmatically.</p>
  
  <p><strong>Delete Entity -</strong> There will be an option to delete the database entity as well. On tapping a button, the name of the entity will be passed to the function and that entity/database-table will be deleted from the database.</p>
  
  <p>I have tried the following code -:</p>
  
  <pre><code>    let model = NSManagedObjectModel()
      let entityName = "TestEntity"
      let entity = NSEntityDescription()
      entity.name = entityName
      entity.managedObjectClassName = entityName
  
      // Creating a attribute
      let remoteURLAttribute = NSAttributeDescription()
      remoteURLAttribute.name = "columnOne"
      remoteURLAttribute.attributeType = .stringAttributeType
      remoteURLAttribute.isOptional = true
      remoteURLAttribute.isIndexed = true
  
      var properties = Array&lt;NSAttributeDescription&gt;()
      properties.append(remoteURLAttribute)
      // Adding the attribute to the entity
      entity.properties = properties
  
      // Adding the entity into the model
      model.entities = [entity]
  
      do {
          try CoreDataManager.managedObjectContext.save()
      } catch {
          print(error)
      }
  
      // Inserting the data into the entity
      let testEntity = NSManagedObject(entity: entity, insertInto: CoreDataManager.managedObjectContext)
      testEntity.setValue("WHY SO SERIOUS?", forKey: "columnOne")
  
      do {
          try CoreDataManager.managedObjectContext.save()
      } catch {
          print(error)
      }
  
      // Fetching the data from the entity
      let request = NSFetchRequest&lt;NSFetchRequestResult&gt;()
      request.entity = entity
      request.propertiesToFetch = ["columnOne"]
      request.returnsDistinctResults = false
      do {
          let results = try CoreDataManager.managedObjectContext.fetch(request)
          print(results)
      } catch {
  
      }
  </code></pre>
  
  <p>I am getting the following value in the <strong>results</strong> -:</p>
  
  <blockquote>
    <p>▿ 1 element
      - 0 :  (entity: TestEntity; id: 0x600000422f80
     ;
    data: {
        columnOne = "WHY SO SERIOUS?"; })</p>
  </blockquote>
  
  <p>But I don't have any way to get the values from this entity anywhere else and insert the values into the entity from other places as well by using entity name which is <strong>TestEntity</strong> in this case.</p>