我们如何正确测试 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/