ios - 我们如何在 Swift 中测试 nils?

标签 ios swift null xcode7

我们如何正确测试 nils?

Xcode 7.01 抛出一个我们以前没有的新错误。

我们收到此错误:

"Binary operator '!=' cannot be applied to operands of type '[NSObject: Anyobject]' and 'NilLiteralConvertible'"

..来自这段代码:

// send request data to phone and handle reply or error
var didOpenParent : Bool = WKInterfaceController.openParentApplication(requestData, reply: { (reply, error) -> Void in

            // if we get a response, handle it appropriately
            if error == nil && reply != nil 
            {
                // code continues

Apple's documentation给出了使用“!=”测试 nils 的例子。尝试一些建议的答案 here例如检查 reply.isEqual(nil) 或 reply.isEmpty 是否对我们不起作用。

我们缺少什么?

最佳答案

reply[NSObject : AnyObject] 类型,它是一个非可选 dictionary类型。您不需要——而且正如您发现的那样,不能——检查它是否为零。

关于ios - 我们如何在 Swift 中测试 nils?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32930470/

相关文章:

iphone - 编辑模式下的自定义 UITableViewCell 不会移动我的 UILabel

ios - 默认 Apple 应用程序的默认 Apple URL 方案是什么?

ios - Swift 3.0 iOS10 通过 Firebase 的 segue 发送器传递异步数据

swift - 在 Swift 中使用函数查找两个序列中的公共(public)元素

scala - 如何在由案例类列表组成的数据集中表示空值

Iphone/ipad 通过触摸事件自定义控制

ios - 带有动态高度单元和动态内容的表格 View 单元格内的 Collection View swift 4

swift - 将 TableView 中的单元格文本打印到新的 viewController UILabel (swift)

javascript - 检查 JavaScript 中两个对象是否都未定义或为 null

swift - Parse & Xcode 6.4 使用 Swift 1.2 展开选项时出现问题