简体   繁体   English

在Swift中检查nil参数以及该参数是否为空字符串

[英]Checking for nil parameter and if that parameter is an empty string in Swift

There may or may not be an easier way to write this, but I feel like being a novice at Swift, I am probably missing something. 也许有或没有一种简单的方法可以编写此代码,但是我觉得自己是Swift的新手,我可能会缺少一些东西。 I have a parameter (fileName) for a method that is an optional String? 我有一个方法(可选String? )的参数(文件名) String? I need to check if it is nil , or if it is an empty String. 我需要检查它是否为nil ,或者是否为空的String。 This is the code I have, and it works fine, but it seems like it could be a bit more concise/readable. 这是我的代码,可以正常工作,但看起来可能更简洁/可读。

func writeFile(fileName: String?, withContents contents: String, errorCallback failureCallback: RCTResponseSenderBlock, callback successCallback: RCTResponseSenderBlock) -> Void {

  // If fileName has a value -- is not nil
  if let fileName = fileName {
    // Check to see if the string isn't empty
    if count(fileName) < 1 {
      // Craft a failure message
      let resultsDict = [
        "success": false,
        "errMsg": "Filename is empty"
      ]

      // Execute the JavaScript failure callback handler
      failureCallback([resultsDict])

      return; // Halt execution of this function
    }
    // else, fileName is nil, and should return the same error message.
  } else {
    // Craft a failure message
    let resultsDict = [
      "success": false,
      "errMsg": "Filename is empty"
    ]

    // Execute the JavaScript failure callback handler
    failureCallback([resultsDict])

    return; // Halt execution of this function
  }
}

How about 怎么样

First create a nil string optional to set up the example var fileName: String? 首先创建一个可选的nil字符串,以设置示例var fileName:字符串?

Now code that lets you see if filename is nil/empty in one simple line: 现在,使您可以在简单的一行中查看文件名是否为nil / empty的代码:

if (fileName ?? "").isEmpty
{
  println("empty")
}
else
{
  println("not empty")
}

This uses ?? 这使用?? , the Swift " nil coalescing operator ". ,Swift的“ 零合并运算符 ”。 (link) (链接)

For the expression: 对于表达式:

a ?? b

Where a is an optional it says: 其中a是可选的,它表示:

if a is not nil, return a. 如果a不为nil,则返回a。 If a is nil, return b instead. 如果a为零,则返回b。

So 所以

if (fileName ?? "").isEmpty

Says

First, evaluate fileName and see if it's nil. 首先,评估fileName并查看它是否为nil。 If so, replace it with an empty string. 如果是这样,请将其替换为空字符串。

Next, check the result to see if it's empty. 接下来,检查结果以查看是否为空。

You have the right approach. 您有正确的方法。 All you need to do is combine your IF statements into one line: 您需要做的就是将IF语句合并为一行:

func writeFile(fileName: String?, withContents contents: String, errorCallback failureCallback: RCTResponseSenderBlock, callback successCallback: RCTResponseSenderBlock) -> Void {
  // Check if fileName is nil or empty
  if (fileName == nil) || (fileName != nil && count(fileName!) < 1) {
    // Craft a failure message
    let resultsDict = [
      "success": false,
      "errMsg": "Filename is empty"
    ]

    // Execute the JavaScript failure callback handler
    failureCallback([resultsDict])

    return; // Halt execution of this function
  }

  // Perform code here since fileName has a value and is not empty
}

This code first checks if fileName is nil . 此代码首先检查fileName是否为nil If so, it goes into the failure block. 如果是这样,它将进入故障块。 If not, it goes to the second conditional. 如果不是,则转到第二个条件。 In the second conditional, it will go into the failure block if fileName has a value and is empty. 在第二个条件中,如果fileName有一个值并且为空,它将进入故障块。 Only when fileName has a value and is NOT empty will it skip the failure block. 只有当fileName有一个值并且不为空时,它才会跳过故障块。

You could combine your if let and if statement into something like this: 您可以将if letif语句组合成这样的东西:

if let str = fileName where !str.isEmpty {
    println(str)
} else {
    println("empty")
}

This approach is useful if you need to use the filename after checking whether it's nil or empty. 如果需要在检查文件名是否为nil或为空后使用文件名,则此方法很有用。

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

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