简体   繁体   English

Swift 3 NSArray比较无

[英]Swift 3 NSArray compare to nil

I'm trying to migrate an objc project to swift3. 我正在尝试将objc项目迁移到swift3。 I'm not sure how can I compare an array to nil. 我不确定如何将数组与nil进行比较。 I have found this topic, but that was 2 years ago and the swift's syntax has changed a lot. 我已经找到了这个主题,但是那是2年前了,而swift的语法已经发生了很大变化。

If I have a code like this in swift: 如果我迅速有这样的代码:

let variable = something as? NSArray
  if variable == nil {
    // do something
  }

It won't let me to compare this variable with nil, causing an error "comparing this variable, always returns false". 它不会让我将此变量与nil进行比较,从而导致出现错误“比较此变量,总是返回false”。 I have tried comparing variable.description with " ", but does it do the same thing? 我曾尝试将variable.description与“”进行比较,但是这样做是否一样? By "something" i meant: 我所说的“某物”是指:

var variable = dict.object(forKey: someString) as! NSArray

The main thing I wanted to do with this was: 我想做的主要事情是:

var variable = dict.object(forKey: someString) as! NSArray
  if variable == nil {
    //create 
  }
  else {
    // append
  }

That's what the optional unwrapping syntax is for. 这就是可选的展开语法的用途。 You can combine the unwrapping and cast into one if statement: 您可以将解包并组合为一个if语句:

if let variable = something as? NSArray {
    // variable is not nil and is an NSArray
    // Now you can do something with it.
} else {
    // Either something is nil or it is not able to be cast as an NSArray
    // Handle this case.
}

I should also mention that if you don't need to use something in Objective-C, then you should use the Swift-native array type. 我还应该提到,如果您不需要在Objective-C中使用something ,那么您应该使用Swift-native数组类型。 This can be declared like this: 可以这样声明:

let someArray = ["string1", "string2"]

This line indicates that variable is and must be an NSArray. 此行表明变量必须是 NSArray。 If dict.object(forKey: someString) is not an NSArray, this will cause a crash 如果dict.object(forKey: someString)不是NSArray,则将导致崩溃

var variable = dict.object(forKey: someString) as! NSArray
//                                               ^
// This exclamation mark means you are certain this is an NSArray
// Also, because there is no question mark after NSArray, this variable
// is not optional. It cannot be nil

However, you then use 但是,您然后使用

if variable == nil {

And this is where the warning comes from. 这就是警告的来源。 The variable can never be nil, because the variable is not optional 该变量永远不能为nil,因为该变量不是optional


What you probably want is: 您可能想要的是:

if let variable = dict.object(forKey:someString) as? NSArray    

This will return false if: 如果满足以下条件,则将返回false:

  • dict.object(forKey:someString) returns a nil object dict.object(forKey:someString)返回一个nil对象
  • the object returned is not an NSArray 返回的对象不是NSArray

After this variable is now a non-optional NSArray. 现在,此variable之后是非可选的 NSArray。 It is guaranteed to be an NSArray and is guaranteed to not be nil . 保证它是一个NSArray,并且保证不为nil You can use it without unwrapping it. 您可以使用它而无需展开它。 eg 例如

if let variable = dict.object(forKey:someString) as? NSArray {
    for element in variable {
    }
}
else {
    //The dict doesn't contain the object yet. `variable` is nil
    //Create a new array and add it to dict
    let newArray = ["First Value"]
    dict[someString] = newArray
}
let variable = something as? NSArray

With this declaration, variable will be an optional type ( NSArray? ) and never nil. 使用此声明, variable将是可选类型( NSArray? ),并且永远不会为零。 This is because casting with as? 这是因为用as?铸造as? returns an optional value that either contains the successfully casted object or nothing. 返回一个可选值,该值要么包含成功转换的对象,要么不包含任何内容。 You can see this by alt -clicking the variable name in Xcode. 您可以按alt -clicking在Xcode变量名看到这一点。

If you want to know whether it contains a value, you need to use the if let syntax: 如果要知道它是否包含值,则需要使用if let语法:

if let variable = variable {
    // variable is guaranteed to be an NSArray here.
}

You can also use this format with guard-else : 您还可以将这种格式用于guard-else

guard let variable = something as? NSArray else {
    // your variable is nil. Do something if needed
}
// your variable is available in this scope. Do something when variable contains Array

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM