代码之家  ›  专栏  ›  技术社区  ›  mfaani

得到“let”模式的警告没有效果;子模式没有绑定任何变量

  •  2
  • mfaani  · 技术社区  · 7 年前

    我正在做以下操作,但得到以下警告:

    enum Seat {
        case middle
        case window
        case aisle(Int)
    }
    
    let m : Seat = .middle
    let w : Seat = .window
    let a : Seat = .aisle(5)
    
    let seats = [m,w,a]
    
    for seat in seats {
        if case let .middle = seat {
            print("middle")
        }
        if case let .window = seat {
            print("window")
        }
        if case let Seat.aisle(row) = seat {
            print("able to let row be the associatedvalue of seat; its value is: \(row)")
        }
    }
    
    3 回复  |  直到 5 年前
        1
  •  6
  •   mfaani    7 年前

    很简单。在这三种情况下,只有 aisle Int . 你的 .middle & .window

    if case let .middle = seat
    

    收件人:

    if case .middle = seat
    

    你的也一样 .窗口 案例

        2
  •  2
  •   mqz.kim    7 年前

    switch

     switch seat {
        case .middle:
            print("middle")
        case .window:
            print("window")
        case let .aisle(row):
            print("able to let row be the associatedvalue of seat; its value is: \(row)")
    }
    
        3
  •  1
  •   vacawama    7 年前

    除了另外两个正确答案,如果你 Seat Equatable ,你可以和 == 对于没有关联值的情况:

    enum Seat: Equatable {
        case middle
        case window
        case aisle(Int)
    }
    
    let m : Seat = .middle
    let w : Seat = .window
    let a : Seat = .aisle(5)
    
    let seats = [m,w,a]
    
    for seat in seats {
        if seat == .middle {
            print("middle")
        }
        else if seat == .window {
            print("window")
        }
        else if case .aisle(let row) = seat {
            print("able to let row be the associatedvalue of seat; its value is: \(row)")
        }
    }
    

    注: if case .aisle(let row) = seat 我认为这使得 let绑定