Saving BackendlessEntity with boolean attribute in Swift

Hi,
I have an Student entity class that stores a boolean parameter called ‘active’. However, when saving the model to the backend, the parameter always gets stored as NULL when I hardcode it to TRUE.
Disclaimer regarding my code, I’m new to Swift…
class Student : BackendlessEntity {

var firstName: String?
var middleName: String?
var lastName: String?
var active: Bool?
}
The save action method looks like this:
@IBAction func saveForm(sender: AnyObject) {
let student = Student()

student.firstName = self.first.text!
student.lastName = self.last.text!
student.middleName = self.middle.text!
student.active = true

let backendless = Backendless.sharedInstance()
backendless.persistenceService.of(Student.ofClass()).save(student, response: { (resondse) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}) { (error: Fault!) -> Void in
// There was a problem, check error.description
print(“Error: (error) (error.description)”)
}
}
The result for activity when saving is done is always NULL. Am I doing something wrong here?

I came across same issue, and found out that the optional “Bool?” was causing the issue. By changing type to “Bool” resolved the issue for me. However, I’m sure there are cases where you wish to handle nil data case hence I also used optional data type in beginning…

<below does not work>

class BooleanTest: BackendlessEntity {
var boolVal: Bool?

override init() {
    boolVal = true
    super.init()
}

}

<This works as expected>

class BooleanTest: BackendlessEntity {
var boolVal: Bool

override init() {
    boolVal = true
    super.init()
}

}

Varuzhan and Hiroaki,

The boolean and numeric (for instance Int) properties cannot be optional with the current implementation in the SDK.

Regards,
Mark

I have a similar problem. I am unable to save an object and the error message lists the four Swift Bool properties:

Unable to save object - invalid data type for properties - attendingComplete, minimallyInvasive, redo, viewedByResident. You can change the property type in developer console.
Here is my class:

class Case: NSObject{

static let sharedInstance = Case()
    
    var objectId = ""
    var created: NSDate?
    var updated: NSDate?
    var caseDate: NSDate?
    var caseOfDay: Int
    var procedureType = ""
    var procedureDetail = ""
    var redo: Bool
    var minimallyInvasive: Bool
    var residentZwischStage = ""
    var residentDifficulty = ""
    var attendingZwischStage = ""
    var attendingDifficulty = ""
    var attendingComments = ""
    var attendingComplete: Bool
    var attendingObject: AllowedUsers?
    var institutionObject: Institution?
    var residentObject: AllowedUsers?
    var viewedByResident: Bool
    var caseDateSummary = ""
    
    override init() {
        redo = false
        minimallyInvasive = false
        attendingComplete = false
        viewedByResident = false
        caseOfDay = 1
        
        super.init()
    }

}

Edward,

Could you post a screenshot of the schema for the Case table?

Regards,
Mark

I am going to make a simpler case. Here is a class called TestTable:

import Foundation

class TestTable: NSObject {

var field1 = ""

var field2: Int

var field3: Bool



override init() {

    field2 = 1

    field3 = false

    

    super.init()

}

}

Here is the method body to save:
let t = TestTable()
t.field1 = “Hello”
t.field3 = true
let datastore = Backendless.sharedInstance().data.of(TestTable.ofClass())
datastore.save(t, response: { (result: AnyObject!) -> Void in
print(“saved”)
}) { (fault: Fault!) -> Void in
print(fault.message)

    }

Here is the fault message:
Unable to save object - invalid data type for properties - field3. You can change the property type in developer console.

If I remove the Bool field, everything saves just fine

Error.txt (588B)

Here is the schema screen shot for TestTablehttp://support.backendless.com/public/attachments/915beabb2dfd74dae252ff494d1f2629.png&lt;/img&gt;

I just tried your code verbatim - no problem, saved an object as expected:

http://support.backendless.com/public/attachments/40027e2c580a4c24191cb24013084022.png&lt;/img&gt;

http://support.backendless.com/public/attachments/d175730c1f705c7e1ecc3fff28753128.png&lt;/img&gt;

Sorry, here is the schema:
http://support.backendless.com/public/attachments/a8374f7ee2386a8a3f51d228e4955a30.png&lt;/img&gt;

The true schema is shown when you click the “Table Schema and Permissions” button, although one can see the data types here as well. Could you email your app id and ios secret key to support@backendless.com? I’d like to run my app against your backend.

id and secret just emailed

Worked just fine - you should see an object saved in the TestTable table.

Try updating the backendless library. Alternatively, try creating a project from scratch, follow the instructions from here and see the minimal code works.

Frustrating. I saw it.

If you delete a table and let the backend create it, do you get the same result as mine? (see the screenshots I attached earlier)

You were able to create a Bool field. But when I created the table de novo, although my field3 is a Bool in my class, the field3 column is an Int in the Backendless console. Image attached. I have re-downloaded all of your files and still get the same result. Remember this is Swift 2.x. Could there be an issue in the datatype mapping from objC to swift?http://support.backendless.com/public/attachments/b0efe9b1436f5e01ef691ace0d51e835.png&lt;/img&gt;

Just to confirm: we run exactly the same code, against the same backend, only in your case Backendless declares field3 as int and in mine it is Boolean?

I hate to say it, but yes. My colleague uses SDK 2.1 and gets a Bool. I am using SDK 3.0.8.2 and get an Int. The workaround is fine for now.

I just re-ran the code with SDK 3.8.0.2 that I re-downloaded from our website. Same result - field3 is created as bool.

I’m still trying to resolve my Swift Bool save issue. I made a new app to save a Bool in one field and an Integer in another field. I downloaded a fresh copy of your iOS SDK. On the simulator, I get an invalid data type for properties error when I try to save to a Boolean field. I tried this on a different Mac and got the same error. Running the program on an actual iPhone with iOS 9.2, the Bool saves without error. When I run it on an actual phone running iOS 8.2 I get the invalid data type error. Does this allow you to have any more insights into the problem?

Hi Edward,

I think we have a pretty good idea how to reproduce the problem. There is an internal ticket assigned to a developer, so it should be fixed soon.

Regards,
Mark