Blog

Declaring Data Classes in Swift 4

by on September 25, 2017

Backendless supports two approaches for saving data objects in the database: the class-based approach and the map/dictionary approach. With the class approach, you declare a class, instances of which represent individual objects stored in the database. With the introduction of Swift 4, it is important to add a new attribute to your Swift 4 classes. For example:

@objcMembers
class Contact : NSObject {
    var objectId : String?
    var age : Int = 0
    var title : String?
    var name : String?
    var phone : String?
}

Notice the @objcMembers attribute declaration in the class. The attribute allows Backendless SDK (which is written in Objective-C) to access the object’s property values. If you do not add the attribute, objects retrieved from the server will not have properties initialized with the values.

It is also possible to set a project-wide configuration property. When you do that, you do not need to add the @objcMembers attribute, however, you will see warnings when the project is built. To set the project property, open “Build Settings” for the project target, locate “Swift 3 @objc inference” and set it to “On”.
swift3-prop-inference
For additional information on the @objcMembers attribute, see the “inheriting from Objective-C Classes” section in the Swift 4 programming guide.

Leave a Reply