简体   繁体   English

有没有办法知道是否将参数(具有默认值)传递给Swift中的函数?

[英]Is there a way to know if a parameter (with a default value) has been passed to a function in Swift?

Here is a function in Swift 这是Swift中的函数

func flexStrings2 (s1 : String = "", s2 :String = "") -> String {
   return s1 + s2 == "" ? "none" : s1 + s2
}

flexStrings2() // returns "none"
flexStrings2(s1: "Hello!") // returns "Hello!"
flexStrings2(s1: "What's ", s2: "up?") // returns "What's up?"
flexStrings2(s1: "", s2: "") // returns "none"

I am trying to write a function called flexStrings2() in Swift that meets the following requirements: 我正在尝试在Swift中编写一个名为flexStrings2()的函数,以满足以下要求:

  • The function can take precisely 0, 1 or 2 string parameters. 该函数可以精确地使用0、1或2个字符串参数。
  • Returns the function parameters concatenated as String. 返回串联为String的函数参数。
  • If no parameters pass to the function, it will return the string “none”. 如果没有参数传递给该函数,它将返回字符串“ none”。
  • Only use one line of code for the function body 函数主体仅使用一行代码

I want flexStrings2(s1: "", s2: "") to return "" and not "none" 我希望flexStrings2(s1: "", s2: "")返回""而不是"none"

can this be done done in one line of code in the function body? 可以在函数体中的一行代码中完成此操作吗?

This is an ugly solution: 这是一个丑陋的解决方案:

func flexStrings2 (s : String...) ->String{
     return s.count == 0 ? "none" : s.count > 2 ? "Maximum 2 parameters" : s.reduce("", combine: {$0 + $1})
}

Plus it doesn't conform to the syntax for calling flexStrings2. 另外,它不符合调用flexStrings2的语法。

So is there a way to know if a parameter (with a default value) has been passed to a function in Swift? 那么有没有办法知道是否将参数(具有默认值)传递给Swift中的函数?

Swift recommends the use of nil to indicate that a variable has no value. Swift建议使用nil来指示变量没有值。 You can use this to distinguish between no value being passed and a blank value being passed, which seems necessary because you want to differentiate between the two. 您可以使用它来区分没有传递值和传递空白值,这似乎是必要的,因为您要区分两者。

func flexStrings2 (s1 : String? = nil, s2 :String? = nil) -> String {
    return (!s1 && !s2) ? "none" : (s1 ? s1! : "") + (s2 ? s2! : "")
}

flexStrings2() // returns "none"
flexStrings2(s1: "Hello!") // returns "Hello!"
flexStrings2(s1: "What's ", s2: "up?") // returns "What's up?"
flexStrings2(s1: "", s2: "") // returns "" like you wanted

This doesn't meet the challenge because it can take zero to many String arguments, but otherwise I think it does what was intended and is easier to read by using a Variadic parameter. 这不能解决挑战,因为它可以接受零到许多String参数,但是否则我认为它可以实现预期的目的,并且通过使用Variadic参数更易于阅读。

func flexStrings(s: String...) -> String {
    return s.count == 0 ? "none" :  join("", s)
}

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

func flexStrings(s1: String = "", s2: String = "") -> String {
    return s1 + s2 == "" ? "none": s1 + s2
}

Is the "answer" for Challenge #2 from... 是挑战#2的“答案”来自...

http://www.raywenderlich.com/76349/swift-ninja-part-1 http://www.raywenderlich.com/76349/swift-ninja-part-1

The challenge is trying to illustrate the use of default values, but it doesn't get into optional parameter values and the use of nil. 挑战在于尝试说明默认值的使用,但它并没有涉及可选参数值和nil的使用。

The answer undetected provided is probably more technically correct, but that's a kind of optional nightmare of ? 从技术上讲,未提供的答案可能在技术上更正确,但这是可选的噩梦? and ! 和! I hope we don't see a lot of in Swift and made more difficult to read because of the use of the nested ternary operators. 我希望我们不会在Swift中看到很多东西,并且由于使用嵌套的三元运算符而使阅读起来更加困难。 The criticism is not towards undetected, but rather picking out ? 批评不是朝着未被发现的方向,而是朝着挑剔的方向发展? and ! 和! in Swift source. 在Swift源码中。

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

相关问题 Swift功能参数默认值 - Swift Function Parameter Default Value Swift:通过NotificationCenter的默认函数参数值 - Swift: Default function parameter value via NotificationCenter Swift-检查对象是否为给定类型(该类型已作为函数参数传递) - Swift - Check if object is of a given type (where the type has been passed as function argument) 当用作函数中的参数时,swift 闭包可以设置为默认值吗? - Can swift closures be set to a default value when used as a parameter in a function? 如何在Swift中的函数中为通用参数返回默认值? - How to return a default value for a generic parameter in a function in Swift? IOS swift如何知道activityViewController何时成功使用 - IOS swift how to know when activityViewController has been successfully used 有没有办法知道UIViewController是否已经以模态方式呈现和解散? - Is there a way to know if a UIViewController has been presented and dismissed modally ? 有没有办法知道何时输入了某个UITextView? - Is there a way to know when a certain UITextView has been entered? 有没有一种简单的方法可以让另一个ViewController知道已经按下了一个按钮? - Is there a simple way to let another ViewController know that a button has been pressed? 具有枚举参数(和默认值)的函数 - Function with Enumeration Parameter (and Default value)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM