Home > Software design >  ParseSwift: how to declare a [String: Any]? property for a ParseObject?
ParseSwift: how to declare a [String: Any]? property for a ParseObject?

Time:04-14

I am trying to use Parse-Swift SDK, and specifically in the app database I have some properties which are dictionaries, like this one:

elements: Dictionary<String, Any>

["test_string": "hello", "test_number": 22.2] // an example of a value for this property

Now I tried to write this ParseObject in Swift:

import Foundation; import ParseSwift

struct TestObject: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?

    var elements: [String: Any]?
}

But doing this, I get these errors:

Type 'TestObject' does not conform to protocol 'Decodable'

Type 'TestObject' does not conform to protocol 'Hashable'

Type 'TestObject' does not conform to protocol 'Encodable'

Type 'TestObject' does not conform to protocol 'Equatable'

What should I do? Thank you for your help

CodePudding user response:

Any isn’t Codable meaning it doesn't conform to the Codable protocol and can't be sent to a Parse Server using JSON which is why the compiler is complaining.

If you want something similar to Any, you can add the AnyCodable package via SPM to your project and use the following types: AnyCodable, AnyEncodable, AnyDecodable. See the notes in the documentation and more info here and here. So your object will look like:

struct TestObject: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?

    var elements: [String: AnyCodable]?
}

To access the value of AnyCodable, AnyEncodable, AnyDecodable; you use the .value property and attempt to cast it to the expected type. You can check the ParseSwift test cases for examples. Using your TestObject type:

var object = TestObject()
object.elements["myKey"] = AnyCodable("My value")
guard let storedValue = object.elements["myKey"]?.value as? String else {
   print("Should have casted to String")
   return
}
print(storedValue) // Should print "My value" to console
  • Related