[英]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.