[英]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()
的函数,以满足以下要求:
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.