Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查nil参数以及该参数在Swift中是否为空字符串_Swift_React Native - Fatal编程技术网

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

检查nil参数以及该参数在Swift中是否为空字符串,swift,react-native,Swift,React Native,可能有也可能没有更简单的方法来写这篇文章,但我觉得作为Swift的新手,我可能遗漏了一些东西。我有一个可选字符串的方法的参数(文件名)我需要检查它是nil还是空字符串。这是我的代码,它工作得很好,但看起来它可能更简洁/可读 func writeFile(fileName: String?, withContents contents: String, errorCallback failureCallback: RCTResponseSenderBlock, callback successCa

可能有也可能没有更简单的方法来写这篇文章,但我觉得作为Swift的新手,我可能遗漏了一些东西。我有一个可选
字符串的方法的参数(文件名)
我需要检查它是
nil还是空字符串。这是我的代码,它工作得很好,但看起来它可能更简洁/可读

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
  }
}
func writeFile(文件名:String?,内容:String,错误回调失败回调:rctreponsesenderblock,回调成功回调:rctreponsesenderblock)->Void{
//如果fileName有一个值--不是nil
如果让fileName=fileName{
//检查字符串是否为空
如果计数(文件名)<1{
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
//否则,fileName为nil,并应返回相同的错误消息。
}否则{
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
}

您的方法是正确的。您只需将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
}
func writeFile(文件名:String?,内容:String,错误回调失败回调:rctreponsesenderblock,回调成功回调:rctreponsesenderblock)->Void{
//检查文件名是否为零或为空
如果(fileName==nil)| |(fileName!=nil&&count(fileName!)<1){
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
//在此处执行代码,因为fileName有一个值且不是空的
}

此代码首先检查
fileName
是否为
nil
。如果是,则进入故障块。如果不是,则转到第二个条件。在第二个条件中,如果
fileName
有值且为空,则它将进入故障块。只有当
fileName
有一个值且不为空时,它才会跳过故障块。

您有正确的方法。您只需将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
}
func writeFile(文件名:String?,内容:String,错误回调失败回调:rctreponsesenderblock,回调成功回调:rctreponsesenderblock)->Void{
//检查文件名是否为零或为空
如果(fileName==nil)| |(fileName!=nil&&count(fileName!)<1){
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
//在此处执行代码,因为fileName有一个值且不是空的
}

此代码首先检查
fileName
是否为
nil
。如果是,则进入故障块。如果不是,则转到第二个条件。在第二个条件中,如果
fileName
有值且为空,则它将进入故障块。只有当
fileName
有一个值且不为空时,它才会跳过故障块。

您有正确的方法。您只需将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
}
func writeFile(文件名:String?,内容:String,错误回调失败回调:rctreponsesenderblock,回调成功回调:rctreponsesenderblock)->Void{
//检查文件名是否为零或为空
如果(fileName==nil)| |(fileName!=nil&&count(fileName!)<1){
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
//在此处执行代码,因为fileName有一个值且不是空的
}

此代码首先检查
fileName
是否为
nil
。如果是,则进入故障块。如果不是,则转到第二个条件。在第二个条件中,如果
fileName
有值且为空,则它将进入故障块。只有当
fileName
有一个值且不为空时,它才会跳过故障块。

您有正确的方法。您只需将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
}
func writeFile(文件名:String?,内容:String,错误回调失败回调:rctreponsesenderblock,回调成功回调:rctreponsesenderblock)->Void{
//检查文件名是否为零或为空
如果(fileName==nil)| |(fileName!=nil&&count(fileName!)<1){
//起草失败消息
让resultsDict=[
“成功”:错误,
“errMsg”:“文件名为空”
]
//执行JavaScript失败回调处理程序
failureCallback([resultsDict])
return;//停止执行此函数
}
//在此处执行代码,因为fileName有一个值且不是空的
}
此代码首先检查
fileName
是否为
nil
。如果是,则进入故障块。如果不是,则转到第二个条件。在第二个条件中,如果
fileName
有值且为空,则它将进入故障块。只有当
fileName
有值且不为空时,它才会跳过故障块。

如何

首先创建一个nil字符串(可选)来设置示例 变量文件名:字符串

现在,您可以在一个简单的示例中查看文件名是否为nil/empty的代码
if let str = fileName where !str.isEmpty {
    println(str)
} else {
    println("empty")
}