swift - 从仅解包选项中向下转换;您是要使用 '!' 吗?

标签 swift swift2

我有这个奇怪的错误,不知道如何修复它。

guard let theDialogObjects = dialogObjects as! [QBChatDialog]

Downcast from '[QBChatDialog]?' to '[QBChatDialog]' only unwraps optionals; did you mean to use '!'?

代码来自这里:https://github.com/QuickBlox/q-municate-services-ios/blob/master/QMChatService/QMChatService/QMChatService.m

- (void)allDialogsWithPageLimit:(NSUInteger)limit
                extendedRequest:(NSDictionary *)extendedRequest
                 iterationBlock:(void(^)(QBResponse *response, NSArray *dialogObjects, NSSet *dialogsUsersIDs, BOOL *stop))iterationBlock
                     completion:(void(^)(QBResponse *response))completion {

这是我在 Swift 中使用它的方式:

QMServicesManager.instance().chatService.allDialogsWithPageLimit(UInt.max, extendedRequest: ["type":String(QBChatDialogType.Private.rawValue)], iterationBlock: { (response, dialogObjects, userIDs, stop) in

      guard let theDialogObjects = dialogObjects as! [QBChatDialog]{
        return
      }

最佳答案

从错误消息中您并没有尝试更改类型,只是检查值不为 nil,因此您的检查应该是:

guard let theDialogObjects = dialogObjects else ...

关于swift - 从仅解包选项中向下转换;您是要使用 '!' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718046/

相关文章:

ios - 使用 swift 并在后台获取数据进行 MVC 的正确方法是什么

ios - fatal error :使用JSON文件和SwiftyJSON解开可选值时意外发现nil

ios - 如何在 iOS 中获取 iPod 库中歌曲的文件位置

swift - 在 Swift 中使用类的方法作为默认参数

json - 我正在尝试使用循环获取存储在 JSONData 中的轨道名称

ios - 是否有任何公共(public) API 可以检测 iOS 16(beta 3)中的锁定模式?

ios - '++' 已弃用 : it will be removed in Swift 3

swift - SwiftUI "body"变量是什么类型?

ios - 如果 Objective-C 文件包含 Project-Swift.h 的导入,如何将其导入到桥接 header

swift - 迁移到 Swift 2 时出现错误 "Ambiguous use of subscript"