Swift Language IOS8 Development battle Note-Core Data4

Source: Internet
Author: User

This part of the back of the video has not spoken, I look at the English book I try to tell you, the book is called "Beginning IOS8 Programming with Swift", interested students themselves search ha. In the previous words we explained the use of COREDADTA, then how to use the CoreData in our project, you can put the code in the previous words in our project appdelegate, the basic set of code is universal, note that there are two sentences to modify, one is:

Let Modelurl = Nsbundle.mainbundle (). Urlforresource ("TableView", Withextension: "MOMD")!

There is one more sentence

Et url = self.applicationDocumentsDirectory.URLByAppendingPathComponent ("Tableview.sqlite")

My project name is TableView (forgive me low a bit), so made the above changes, the other unchanged, are from our new CoreData project copied over, the code after copying, remember to add:

Import CoreData

This is CoreData's header file, and now all the preparations for using CoreData are done, so let's start creating the model we need. We've been through this before,



In the build, a new rest entity corresponds to the rest class in our project, and just click + Add.

People may wonder why the type of image is binary Data, in fact, because the previous practice of our pictures are placed in the app, so we use the picture in the UIImage class directly with the name of the image can be found, if the user imported new photos from the camera, We can no longer use this method, so we need to save the image in the database, and using binary data is the purpose. Then we need to turn our rest class into a class that is managed by CoreData, opening the rest class and making the following modifications:

Import Foundationimport coredataclass rest:nsmanagedobject {        @NSManaged var name:string!    @NSManaged var type:string!    @NSManaged var location:string!    @NSManaged var image:nsdata!    @NSManaged var isvisit:nsnumber!    }
The function of the keyword @nsmanaged is to communicate with the corresponding attribute in the entity, Binary data corresponds to the type of Nsdata,core data does not have a Boolean attribute, can only be distinguished by 0 and one. When you have modified the class, create a mapping between the class and the entity. Do the following to enter the class name:

After the establishment, because some of the code we wrote earlier does not apply to CoreData, the compiler will make an error, and now it is one by one resolved. First you can't load the picture with the file name now. Picture now when the data object, the previous code is as follows:

Cgimageview?. Image = UIImage (named:newRest.image)

Modify the following:

Cgimageview?. Image = UIImage (data:newRest.image)

The second error is the Isvisit property, which is no longer a Boolean type, before the code:

let here = rest!. Isvisit? "Yes,i m Here": "No"

Modified to:

            let here = rest!. Isvisit.boolvalue? "Yes": "No"

We need to get a Boolean value for it

The third mistake is that we can't use the previous DataArray now, we emptied it. Now that we have no data in our program, we need to add the data to the database ourselves.



Swift Language IOS8 Development battle Note-Core Data4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.