在这里它说:"注意:_意思是"我不关心那个价值",但是来自JavaScript,我不明白这意味着什么.
我能够打印这些函数的唯一方法是在参数之前使用下划线:
func divmod(_ a: Int, _ b:Int) -> (Int, Int) {
return (a / b, a % b)
}
print(divmod(7, 3))
print(divmod(5, 2))
print(divmod(12,4))
Run Code Online (Sandbox Code Playgroud)
如果没有下划线,我必须这样写它以避免任何错误:
func divmod(a: Int, b:Int) -> (Int, Int) {
return (a / b, a % b)
}
print(divmod(a: 7, b: 3))
print(divmod(a: 5, b: 2))
print(divmod(a: 12,b: 4))
Run Code Online (Sandbox Code Playgroud)
我不明白这个下划线用法.何时,如何以及为何使用这些下划线?
在我的项目中,在转换为swift 3之后,我的ViewController课程出现了一个新功能:
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
Run Code Online (Sandbox Code Playgroud)
这个功能有什么作用?我为什么需要它?
我正在对Alamofire进行一些研究,我遇到了这段代码:
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
if let info = response.result.value as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
print("LINK: \(imgLink)")
}
}
}
} case .Failure(let error):
print(error)
}
Run Code Online (Sandbox Code Playgroud)
我可以知道_,_是什么意思吗?
我已经看过它的用途,let _ = "xyz"但我从来没有像上面的代码那样使用它.
这是否意味着它有2个未被使用的参数?
提前致谢