相关疑难解决方法(0)

为什么我需要快速下划线?

在这里它说:"注意:_意思是"我不关心那个价值",但是来自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

131
推荐指数
4
解决办法
3万
查看次数

转换为swift 3后,视图控制器中出现奇怪的通用函数

在我的项目中,在转换为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)

这个功能有什么作用?我为什么需要它?

ios swift swift3

39
推荐指数
1
解决办法
3124
查看次数

斯威夫特:"_,_"是什么意思?

我正在对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个未被使用的参数?

提前致谢

swift alamofire

1
推荐指数
1
解决办法
203
查看次数

标签 统计

swift ×3

alamofire ×1

ios ×1

swift3 ×1