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. However, when I try to append the individual values of the array I get this error: 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.
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]!
.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.