Core data is used to persist data, which is a technology based on the SQLite database.
So how is it implemented in Swift?
First, you need to create a new template, open the project in the Xcdatamodeld file, click "Add Entity", this time, the creation of a template. After that, you can modify the name of the template to the name you want. Then, in attributes, click "+", add the field and modify the type.
Then, in the code "import CoreData". Next, save the object with Nsmanagedobject, which can be converted to any object. Its type is a dictionary.
To read data from an existing list:
var fileslist = [Nsmanagedobject] ()
String name = Filelist[0].valueforkey ("name") as String?
Save the sample, divided into five steps:
Save data to Entity
Func savefiles (name:string, content:string) {
The first step is to get the core data agent:
Let appdelegate = Uiapplication.sharedapplication (). Delegate as Appdelegate//General Agent
Step two, get the managedobject.
Let Managedcontext = Appdelegate.managedobjectcontext
The third step is to get the entity:
Let entity = Nsentitydescription.entityforname ("article", inmanagedobjectcontext:managedcontext!)
The fourth step is to initialize the object to be inserted and set the value of the object:
Let file = Nsmanagedobject (entity:entity!, Insertintomanagedobjectcontext:managedcontext)
File.setvalue (name, Forkey: "Name")
File.setvalue (Content, Forkey: "Content")
Fifth Step, Save:
var error:nserror? = Nil
managedcontext!. Save (&error)
Sixth step, save the data to the list
Self.filesTable.insert (file, atindex:0)
}
One of Core data use (Swift): Save