1

I want to store status (true/false/unknown) of the sensor, I have created php API to return JSON value without any issues but I can't store empty value, NULL value (nil value in swift ) in Bool variable. In this case, whether I need to define var String to store ON/OFF/Unknown value or I can use Bool var to store true/false/nil?

I define Struct with Codable

struct nodedata: Codable {
var nodeName: String
var nodeID: String
var temperature: Float
var humidity: Float
var relayStatus: Bool
var lightStatus: Bool
var hallStatus: Bool
var smokeStatus: Bool
var pirStatus: Bool
enum CodingKeys: String, CodingKey {
        case nodeName = "node_name"  //Custom keys
        case nodeID = "node_id"
        case temperature = "temp"
        case humidity = "hum"
        case relayStatus = "relay_status"
        case lightStatus = "light_status"
        case hallStatus = "hall_status"
        case smokeStatus = "smoke_status"
        case pirStatus = "pir_status"
    }
}

below is the class to store value get from JSON

class DataManager {
    var nodes = [nodedata]()    // i write main code to store JSON only...
guard let data = data else { return }   // data get from URLSession
                        print(data)
                        let decoder = JSONDecoder()
                        self.nodes = try decoder.decode([nodedata].self, from: data)

I add JSON return from server

[
  {
    "node_name": "SVIN03",
    "node_id": "y2cfwecrw3hqznuxmfvf",
    "temp": 2132,
    "hum": 111,
    "pir_status": false,
    "smoke_status": false,
    "light_status": false,
    "hall_status": false,
    "relay_status": false
  },
  {
    "node_name": "SVIN04",
    "node_id": "aj2w1aljw8nd65ax79dm",
    "temp": 0,
    "hum": 0,
    "pir_status": false,
    "smoke_status": false,
    "light_status": false,
    "hall_status": false,
    "relay_status": false
  },
  {
    "node_name": "SVIN05",
    "node_id": "mwmfl2og2l8888fjpj2d",
    "temp": 999,
    "hum": 0,
    "pir_status": true,
    "smoke_status": false,
    "light_status": false,
    "hall_status": false,
    "relay_status": false
  }
]
6
  • 2
    In Swift, you can use a Bool? variable.
    – Amadan
    Oct 17, 2018 at 7:08
  • Please give an example of the JSON that's causing you problems Oct 17, 2018 at 7:22
  • Swift Tip: In your case, there's no need to declare CodingKeys. Set decoder.keyDecodingStrategy = .convertFromSnakeCase instead. Also, use let in your struct rather than var Oct 17, 2018 at 7:23
  • @AshleyMills i have add JSON return from server in my post. i have question about, if sensor in unknown value, what should i return in JSON ? String type or Bool type ?
    – Anh Le
    Oct 17, 2018 at 7:49
  • @AshleyMills if PHP return String type ( ON/OFF/Unknown) value, in Xcode i define String type to get the status. is this a good way to know 3 states of sensor ?
    – Anh Le
    Oct 17, 2018 at 7:50

1 Answer 1

1

If you have three states of the sensor, its not a good way to use Boolean, instead you can use integer flags, which tells sensor states.

2
  • can you give some code example ? thank you very much
    – Anh Le
    Oct 17, 2018 at 8:09
  • var lightStatus: Int instead of var lightStatus: Bool i.e. Oct 17, 2018 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.