parseswift:如何声明[字符串:任何]? parseobject的属性?

发布于 2025-01-21 04:46:27 字数 730 浏览 3 评论 0原文

我正在尝试使用parse-swift sdk,特别是在应用数据库中,我有一些词典,例如这个词:

elements: Dictionary<String, Any>

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

现在,我尝试在此中编写此parseObject Swift:

进口基础;导入parseswift,

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

    var elements: [String: Any]?
}

但这样做,我得到了这些错误:

键入'testObject'不符合协议'可解码'

键入'testObject'不符合协议'hashable'

键入'testObject'不符合协议'编码'

键入'testObject'不符合协议'equatable'

'我愿意?感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2025-01-28 04:46:27

任何不是代码这意味着它不符合codable协议,也不能使用JSON发送到解析服务器为什么编译器抱怨。

如果您想要类似于任何的东西,则可以添加 anycodable 通过SPM包给您的项目,并使用以下类型:任何可编码任何codobleanydecodable。请参阅和更多信息在这里 href =“ https://github.com/parse-community/parse-swift/issues/141#issue-890290238” rel =“ nofollow noreferrer”>在这里。因此,您的对象将看起来像:

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

    var elements: [String: AnyCodable]?
}

要访问任何可编码nyencodableanydecodable;您使用.value属性,并尝试将其投放到预期类型。您可以检查 parseswift test案例以获取示例。使用您的testObject类型:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文