ios 如何访问“Any”类型对象的属性?

mspsb9vt  于 2022-11-19  发布在  iOS
关注(0)|答案(1)|浏览(92)

下面是一个方法调用,我试图访问r对象的属性,但我不知道如何访问它们。

methodChannel.invokeMethod("DoSomething",arguments: link, result: { (r: Any?) -> () in
    debugPrint("invoked method")
    // TODO: How do I access r's properties?
})

下面是它在调试器查看器中的外观:

如何访问r对象中的message属性?

irtuqstp

irtuqstp1#

你不能访问Any的属性,因为它没有属性--它只是代表一个未指定的类型。要访问示例的属性,你需要将它强制转换为具体类型。
从调试器输出的外观来看,基础类型是FlutterError,因此您可以访问它的属性,如

if let error = r as? FlutterError {
  // access any properties of FlutterError
} else {
  //handle it not being the FlutterError type you expected!
}

相关问题