@RawValue注释不适用于目标值参数

Tar*_*run 8 android kotlin

我正在尝试Parcelize数据类.它包含一个参数:

var tokenType: Any? = null
Run Code Online (Sandbox Code Playgroud)

对于这个变量编译器在编译时抱怨:

Type is not directoly supported by Parcelize. 
Annotate the parater with @RawValue if you want it to be serialized via 
writeValue()
Run Code Online (Sandbox Code Playgroud)

虽然错误是自我解释的,但当我添加@RawValue时:

@RawValue var tokenType: Any? = null
Run Code Online (Sandbox Code Playgroud)

它给出了一个错误:

This annotation is not applicable to target value parameter
Run Code Online (Sandbox Code Playgroud)

有关如何处理此问题的任何提示?

Tar*_*run 23

我从Kotlang社区得到了这个问题的答案.答案是您无法对变量本身进行注释,但您必须对其类型进行注释.

因此,通过以下方式进行注释可以消除错误:

 var tokenType: @RawValue Any? = null
Run Code Online (Sandbox Code Playgroud)

虽然不要忘记手动为此属性编写serilizer/deserializer,因为它不会自动完成.

希望能帮助到你.

  • 如何编写 Any 类型的序列化器/反序列化器? (4认同)
  • 看到此答案后,我立即意识到了解目标注释的含义,谢谢! (2认同)