parseswift:如何声明[字符串:任何]? parseobject的属性?
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何
不是代码
这意味着它不符合codable
协议,也不能使用JSON发送到解析服务器为什么编译器抱怨。如果您想要类似于
任何
的东西,则可以添加 anycodable 通过SPM包给您的项目,并使用以下类型:任何可编码
,任何codoble
,anydecodable
。请参阅和更多信息在这里 href =“ https://github.com/parse-community/parse-swift/issues/141#issue-890290238” rel =“ nofollow noreferrer”>在这里。因此,您的对象将看起来像:要访问
任何可编码
,nyencodable
,anydecodable
;您使用.value
属性,并尝试将其投放到预期类型。您可以检查 parseswift test案例以获取示例。使用您的testObject
类型:Any
isn’tCodable
meaning it doesn't conform to theCodable
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: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 yourTestObject
type: