为啥类型转换在 Swift 2.0 中不起作用?

Posted

技术标签:

【中文标题】为啥类型转换在 Swift 2.0 中不起作用?【英文标题】:Why does type casting not work the same in Swift 2.0?为什么类型转换在 Swift 2.0 中不起作用? 【发布时间】:2015-11-08 22:06:36 【问题描述】:
firebase!.observeEventType(FEventType.Value, withBlock:  [weak self] (snapshot) in    
    if let stuff: AnyObject = snapshot.value 
            let from_user_id = stuff["from_user_id"] as? Int //warning
            let value = stuff["value"] as? Int  //warning

        

我收到警告:

Cast from 'MDLMaterialProperty?!' to unrelated type 'Int' always fails

observeEventType 声明为:

func observeEventType(eventType: FEventType, withBlock block: ((FDataSnapshot!) -> Void)!) -> UInt
Description 
observeEventType:withBlock: is used to listen for data changes at a particular location. This is the primary way to read data from Firebase. Your block will be triggered for the initial data and again whenever the data changes.
Use removeObserverWithHandle: to stop receiving updates.
Parameters  
eventType   
The type of event to listen for.
block   
The block that should be called with initial data and updates. It is passed the data as an FDataSnapshot.
Returns 
A handle used to unregister this block later using removeObserverWithHandle:
Declared In Firebase.h

snapshot.value 定义为:

var value: AnyObject!  get 
Description 
Returns the contents of this data snapshot as native types.
Data types returned: * NSDictionary * NSArray * NSNumber (also includes booleans) * NSString
Returns 
The data as a native object.
Declared In FDataSnapshot.h

【问题讨论】:

为什么你不使用真正的'stuff'类型而不是使用AnyObject?当我修改代码时,你应该知道在执行 'if let stuff: put_here_the_specific_type' 时什么是 'stuff' 类型。 @CiprianC snapshot.value 声明为var value: AnyObject! get @TIMEX 您应该显示您对MDLMaterialProperty 的定义。在不知道其中的类型的情况下,我们只能猜测发生了什么。 能不能把函数observeEventType的定义放上去? @DanielZhang 我正在使用来自 cocoapods 的 Firebase 模块。我不知道 MDLMaterialProperty 是什么,但这是来自一个基本的 Firebase 示例。 【参考方案1】:

您不能为 AnyObject 下标。必须将其转换为字典。 您还需要确保这些值是 Ints。

  firebase!.observeEventType(FEventType.Value, withBlock:  [weak self] (snapshot) in    
            if let stuff = snapshot.value as? [String:AnyObject] 
                    let from_user_id = stuff["from_user_id"] as? Int
                    let value = stuff["value"] as? Int
                
        

【讨论】:

我明白了:无法使用类型为“(MDLMaterialProperty?!)”的参数列表调用类型“Int”的初始化程序。我还希望这两个变量是可选的。 好吧,AnyObject 不能转换为 Int,因为 Int 不是对象。 Cast from 'MDLMaterialProperty?!' to unrelated type 'NSNumber' always fails Cannot convert value of type 'AnyObject!' to specified type '[String : AnyObject]?'【参考方案2】:

试试这个:

firebase!.observeEventType(FEventType.Value, withBlock:  [weak self] (snapshot) in
    if let stuff = snapshot.value as? NSDictionary 
        if let from_user_id = stuff["from_user_id"] as? Int 
            // Do something with from_user_id.
        
        if let value = stuff["value"] as? Int 
            // Do something with value.
        
    

在 Swift 2 中从 AnyObject 进行强制转换时,使用可选绑定将是处理潜在 nil 值以及潜在不兼容类型的最安全方法。

【讨论】:

Cannot convert value of type 'AnyObject!' to specified type 'NSDictionary?' @TIMEX 我已将其更新为使用可选演员表。让我知道这是否有效。

以上是关于为啥类型转换在 Swift 2.0 中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

为啥浮点数据类型在没有类型转换 4.7 的情况下在此代码中不起作用? [复制]

UIDevice 在 swift 2.0 中不起作用

搜索栏在 swift 3 中不起作用。无法将类型 (_) -> Bool 的值转换为预期的参数类型 NSPredicate

为啥 private(set) 在 Swift 中不起作用?

为啥速记参数名称在这个 Swift 闭包中不起作用?

为啥两个表格视图单元格中的两个集合视图在 Swift 4 中不起作用?