gpt4 book ai didi

swift - 使用#line 获取下标中的行号

转载 作者:可可西里 更新时间:2023-11-01 02:16:41 25 4
gpt4 key购买 nike

我想获取调用下标进行调试的行号(如索引超出范围等):

/* 1*/   struct Collection {
/* 2*/ // this works flawlessly
/* 3*/ func getElement(index: Int, line: UInt = #line) -> Double {
/* 4*/ print(line) // 16
/* 5*/ return 0.0
/* 6*/ }
/* 7*/
/* 8*/ // error: Default argument is only permitted for a non-curried function parameter
/* 9*/ subscript(index: Int, line: UInt = #line) -> Double {
/*10*/ print(line) // should print 17
/*11*/ return 0.0
/*12*/ }
/*13*/ }
/*14*/
/*15*/ let c = Collection()
/*16*/ c.getElement(1)
/*17*/ c[1]

如您所见,下标版本不起作用。

是否有解决此限制的方法?

最佳答案

我很惊讶这是不可能的!我刚刚自己测试了这个(使用 Swift 2.2),我可以确认这会发生。

似乎没有解决方法。它可能是也可能不是错误。如果您认为此错误是无意的,我鼓励您在 https://bugs.swift.org 提交错误。 .


编辑: @MartinR 正确地指出了一些 documentation在评论部分:

Subscripts can take any number of input parameters, and these input parameters can be of any type. Subscripts can also return any type. Subscripts can use variable parameters and variadic parameters, but cannot use in-out parameters or provide default parameter values.

有意的行为。

关于swift - 使用#line 获取下标中的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com