简体   繁体   English

从'()'转换为不相关的类型'String'总是失败

[英]Cast from '()' to unrelated type 'String' always fails

I'm trying to step through an [AnyObject] array from a completion block and cast the items as Strings so that I can then put them in a tableView. 我试图从完成块中逐步通过[AnyObject]数组,并将项目转换为字符串,以便可以将其放入tableView中。 However, when I try to append the individual values of the array I get this error: Cast from '()' to unrelated type 'String' always fails . 但是,当我尝试附加数组的单个值时,出现此错误: Cast from '()' to unrelated type 'String' always fails Here is the code: 这是代码:

client.historyForChannel(ids, start: nil, end: nil, withCompletion: { (result, status) -> Void in

    if status == nil {
        if result!.data.messages.count > 0 {
            let historyMessages = result!.data.messages as? [String]
            for value in historyMessages!{
                self.messagesArray.append(value) as? String //error
            }
        }
    }
})

If it helps, I'm using PubNub to create/store messages in my Swift app. 如果有帮助,我正在使用PubNub在我的Swift应用程序中创建/存储消息。

Thanks! 谢谢!

When you wrote 当你写

self.messagesArray.append(value) as? String

you probably meant 你可能是说

self.messagesArray.append(value as? String)

although 虽然

self.messagesArray.append(value)

should suffice because historyMessages is already of type [String]! 应该足够,因为historyMessages已经是[String]!类型[String]! .

The error is saying that you are casting the result of self.messagesArray.append(value) (which is Void because append does not return anything) to String , which does always fail. 该错误表明您将self.messagesArray.append(value) (由于append未返回任何内容,所以为Void self.messagesArray.append(value)的结果强制转换为String ,但始终会失败。

As an aside, your code uses way more exclamation points than it should. 顺便说一句,您的代码使用了更多的感叹号。 You should be using guard-let to make sure your variables are non-nil. 您应该使用guard-let来确保变量为非零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从“字符串?”投射 到不相关类型'[[String:AnyObject]]'总是失败 - Cast from 'String?!' to unrelated type '[[String : AnyObject]]' always fails 从“ [任何]?”演员表 到不相关的类型'[String:String?]'总是失败 - Cast from '[Any]?' to unrelated type '[String : String?]' always fails issue 从[String:AnyObject]强制转换为不相关的类型NSMutableDictionary始终失败警告 - cast from [String:AnyObject] to unrelated type NSMutableDictionary always fails Warning 从FIRRemoteConfigValue转换为不相关的类型String总是失败:Firebase,Swift - Cast from FIRRemoteConfigValue to unrelated type String always fails : Firebase, Swift 从“字符串”强制转换为不相关的类型“ LocationModel”始终失败警告 - Cast from 'String' to unrelated type 'LocationModel' always fails warning 来自'FIRRemoteConfigValue!'不相关的类型'String'总是失败 - Cast from 'FIRRemoteConfigValue!' to unrelated type 'String' always fails 从“TCHMessageType”转换为无关类型“String”总是失败 - Cast from 'TCHMessageType' to unrelated type 'String' always fails 从'[[String:Any]]'转换为不相关的类型'[String:Any]'在FireBase Swift中始终失败 - Cast from '[[String : Any]]' to unrelated type '[String : Any]' always fails in FireBase Swift Swift:从“Ride”转换为不相关的类型“NSDictionary”总是失败 - Swift: Cast from 'Ride' to unrelated type 'NSDictionary' always fails 从“ NSPersistentStoreResult”强制转换为无关类型“ [entity]”始终失败 - Cast from 'NSPersistentStoreResult' to unrelated type '[entity]' always fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM