简体   繁体   English

为什么XCode 6中的iOS应用示例会检查非可选变量的nil?

[英]Why sample iOS app in XCode 6 checks for nil of a non-optional variable?

In "Master-Details" sample Swift app that comes with XCode 6 in MasterViewController.swift file they define objects like this: 在“Master-Details”示例中,在MasterViewController.swift文件中随附XCode 6的Swift应用程序,它们定义了如下objects

var objects = NSMutableArray()

Then in insertNewObject method they check against nil before using it: 然后在insertNewObject方法中,他们在使用之前检查nil

func insertNewObject(sender: AnyObject) {
    if objects == nil {
        objects = NSMutableArray()
    }
    objects.insertObject(NSDate.date(), atIndex: 0)
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

If objects is not optional and objects = nil throws an error, why do they need to guard against nil ? 如果objects不是可选的并且objects = nil会抛出错误,为什么他们需要防范nil

That is a bug; 那是一个错误; I find it surprising that it compiles without an error or warning. 我发现它编译没有错误或警告令人惊讶。 (I guess it's probably being turned into a call to isEqual:, passing nil?) Interestingly, the more idiomatic version: (我猜它可能会变成对isEqual:的调用,传递nil?)有趣的是,更惯用的版本:

if objects {
    objects = NSMutableArray()
}

Does actually fail; 实际上是失败了; you get an error on the if objects line because you can't test an NSMutableArray for boolean-ness. 你在if objects行上得到一个错误,因为你无法测试NSMutableArray的boolean-ness。

You don't need to guard against nil . 你不需要防范nil That's the entire point of optionals (or non-optionals). 这是期权(或非期权)的全部观点。 Also, in Swift, when you are testing for nil , if the variable is an optional (which is the only time it could be nil anyhow), you can just say: 此外,在斯威夫特,当您正在测试的nil ,如果变量是一个可选的(这也可能是唯一一次nil无论如何),你可以说:

if optionalVar { 
    // do stuff
}

This is probably just translated from the Objective-C version by Xcode and isn't made "Swifty." 这可能只是由Xcode从Objective-C版本翻译而来,并不是“Swifty”。

Another instance of this kind of thing: 这种事的另一个例子:

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    // Custom initialization
}

The // Custom initialization should go above the super.init() in Swift, but this is just translated from the Objective-C version. // Custom initialization应该高于Swift中的super.init() ,但这只是从Objective-C版本翻译而来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非可选类型可以为零吗? - Can a non-optional type be nil? 为什么 Swift Array contains(_:) 方法在必须是非可选参数时接受 nil 参数? - Why does Swift Array contains(_:) method accept nil argument when it must be non-optional? Xcode NSManagedObject 子类在标记为非可选时包含可选 - Xcode NSManagedObject subclass contains optionals when they are marked as non-optional nil 合并运算符 '??' 的左侧具有非可选类型“字符串”,因此从不使用右侧 - Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used 将“JSON”类型的非可选值与“nil”进行比较总是返回 true。 有什么建议可以解决这个问题吗? - Comparing non-optional value of type 'JSON' to 'nil' always returns true. Any suggestion to fix this? iOS核心数据-轻量级迁移,可将关系从非可选更改为可选 - iOS Core Data - Lightweight migration for changing relationship from non-optional to optional 将非可选变量更改为Swift可选类型会破坏其在代码中的所有出现 - Changing non-optional variable to Swift optional type breaks all its occurrences in code 致命错误:解开可选值时意外发现nil(无法强制解开非可选类型'String'的值) - fatal error: unexpectedly found nil while unwrapping an Optional value (cannot force unwrap value of non-optional type 'String') 如何使用非可选属性对UITableViewController进行子类化 - How to subclass UITableViewController with non-optional property 在Swift中使用.Success枚举返回非可选 - Return non-optional with .Success enum in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM