Swift 滥用guard语句来代替零检查

Swift 滥用guard语句来代替零检查,swift,linked-list,swift2,switch-statement,guard-statement,Swift,Linked List,Swift2,Switch Statement,Guard Statement,为了适应Swift(来自objc),我正在做一些非常简单的事情——我想使用guard语句和switch语句返回链表中所需的节点。我显然误用了guard语句,因为我的else子句很大(它是保存switch语句的地方)。也许我甚至不需要一个switch语句,但它只是稍微清理了一下 我的旧代码如下: func getValue (atIndex index: Int) -> T { if count < index || index < 0 { print (

为了适应Swift(来自objc),我正在做一些非常简单的事情——我想使用
guard
语句和
switch
语句返回链表中所需的节点。我显然误用了
guard
语句,因为我的
else
子句很大(它是保存switch语句的地方)。也许我甚至不需要一个
switch
语句,但它只是稍微清理了一下

我的旧代码如下:

func getValue (atIndex index: Int) -> T {
    if count < index || index < 0 {
        print ("index is outside of possible range")
    }
    var root = self.head
    //        if var root = self.head {
    if index == 0 {
        return (self.head?.value)!
    }
    if index == count-1 {
        return (self.tail?.value)!
    }
    else {
        for _ in 0...index-1 {
            root = root!.next!
        }
    }
    return root!.value
}

我想在我的
guard
语句之外添加一个
print
语句,说明所需索引超出范围,但我还需要在
T
类型的函数末尾返回一些内容。问题在于,在我的
guard
和switch语句之外,我没有任何东西可以返回。

guard
语句用于捕获无效的情况,因此您可能需要如下内容:

func getValueAtIndex(index: Int) -> T {
    guard index >= 0 && index < count else {
        // Invalid case
        print("Index is outside of possible range")

        // Guard must return control or call a noreturn function.
        // A better choice than the call to fatalError might be
        // to change the function to allow for throwing an exception or returning nil.
        fatalError("Index out of bounds")
    }

    // Valid cases
}
func getValueAtIndex(索引:Int)->T{
保护索引>=0&&index
使用
guard
语句捕获无效案例,因此您需要类似以下内容:

func getValueAtIndex(index: Int) -> T {
    guard index >= 0 && index < count else {
        // Invalid case
        print("Index is outside of possible range")

        // Guard must return control or call a noreturn function.
        // A better choice than the call to fatalError might be
        // to change the function to allow for throwing an exception or returning nil.
        fatalError("Index out of bounds")
    }

    // Valid cases
}
func getValueAtIndex(索引:Int)->T{
保护索引>=0&&index
如果发现值为
nil
,则
guard
语句用于将程序移出其当前作用域或调用
noreturn
函数。但是,您正在运行
保护
中的整个
开关
语句

根据:

guard
语句的
else
子句是必需的,必须调用标有
noreturn
属性的函数,或者使用以下语句之一将程序控制转移到guard语句的封闭范围之外:

  • 返回
  • 中断
  • 继续
防护装置的一个好例子可能是:

var optValue : String?

guard let optValue = optValue else {return}

guard
语句用于将程序移出其当前作用域,或者在发现值为
nil
时调用
noreturn
函数。但是,您正在运行
保护
中的整个
开关
语句

根据:

guard
语句的
else
子句是必需的,必须调用标有
noreturn
属性的函数,或者使用以下语句之一将程序控制转移到guard语句的封闭范围之外:

  • 返回
  • 中断
  • 继续
防护装置的一个好例子可能是:

var optValue : String?

guard let optValue = optValue else {return}

我想你误解了警卫的用法。你在守卫中的条件是成功的条件。你放在防护装置主体中的东西是发生故障时要执行的代码,这必须导致控制权的转移(
返回
抛出
等)。因此,成功代码应该紧跟在警卫后面。同样,切换
true
也没有意义,当然切换
index
会更有意义,因为这就是您要比较的?还有什么是
T
?您的函数不是泛型的,是泛型的吗?当我尝试切换索引时,遇到错误消息“类型为'Bool'的表达式模式无法匹配类型为'Int'的值”。链表类是通用的-在实例化时,我指定希望每个节点值是什么类型的对象。您需要使用
索引的可能值作为案例,即
案例0
案例计数-1
。您应该接受CharlesA的答案,但有一些不相关的观察:首先,
getValue
似乎正在从链接列表中检索项目。与其修改名称以符合标准(就像CharlesA那样,将其重命名为
getValueAtIndex
),我只想将其设为
下标
。其次,
开关真{…}
是冗余的。实际上,我会丢失
if
语句,并在
switch index{…}
中使用有意义的
case
值。第三,代码中有一些不返回值的路径。例如,你看,我认为你误解了警卫的用法。你在守卫中的条件是成功的条件。你放在防护装置主体中的东西是发生故障时要执行的代码,这必须导致控制权的转移(
返回
抛出
等)。因此,成功代码应该紧跟在警卫后面。同样,切换
true
也没有意义,当然切换
index
会更有意义,因为这就是您要比较的?还有什么是
T
?您的函数不是泛型的,是泛型的吗?当我尝试切换索引时,遇到错误消息“类型为'Bool'的表达式模式无法匹配类型为'Int'的值”。链表类是通用的-在实例化时,我指定希望每个节点值是什么类型的对象。您需要使用
索引的可能值作为案例,即
案例0
案例计数-1
。您应该接受CharlesA的答案,但有一些不相关的观察:首先,