【问题标题】:Swift: Using `var` leads to a compiler warning, using `let` leads to compiler errors?Swift:使用`var`会导致编译器警告,使用`let`会导致编译器错误?
【发布时间】:2019-02-03 12:24:45
【问题描述】:

我定义了一个CanStack 协议,其关联类型名为Item:

CanStack.swift

// protocol definition
protocol CanStack: 
  ExpressibleByArrayLiteral, CustomStringConvertible
{
  associatedtype Item
  var items:[Item] { get }
  init()
  mutating func push(_ items: [Item])
  mutating func pop() -> Item?
}

// protocol extension (default behavior)
extension CanStack {

  public var isEmpty: Bool { return items.isEmpty }

  // init by array
  public init(_ items:[Item]) {
    self.init()
    self.push(items)
  }

  // init by variadic parameter
  public init(_ items:Item...){
    self.init()
    self.push(items)
  }

  // push items by variadic parameter
  public mutating func push(_ items:Item...){
    self.push(items)
  }

}

// conform to ExpressibleByArrayLiteral 
extension CanStack {
  public init(arrayLiteral items:Item...){
    self.init()
    self.push(items)
  }
}

// conform to CustomStringConvertible
extension CanStack {
  public var description: String {
    return "["
      + items.map{"\($0)"}.joined(separator:", ")
      + " ⇄ in/out"
  }
}

并定义了一个符合该协议的StackStruct结构体,这个泛型结构体有一个类型参数Item(与上面关联的类型完全相同):

StackStruct.swift

public struct StackStruct<Item> {

    public private(set) var items = [Item]()
    public init() { }

    mutating public func push(_ items:[Item]) {
        self.items += items
    }

    @discardableResult
    mutating public func pop() -> Item? {
        return items.popLast()
    }

}

// adopt CanStack protocol
extension StackStruct: CanStack { }

然后我定义了另一个符合协议的类Stack:

堆栈.swift

public class Stack<Item> {

    public private(set) var items = [Item]()
    public required init() {}

    public func push(_ newItems:[Item]) {
        items += newItems
    }

    @discardableResult
    public func pop() -> Item? {
        return items.popLast()
    }

}

// adopt CanStack protocol
extension Stack: CanStack { }

我有 3 个测试用例:

TestCases.swift


func testStackStruct() {
    // init
    var s1: StackStruct = [1,2,3] // expressible by array literal
    var s2 = StackStruct([4,5,6]) // init by array
    var s3 = StackStruct(7, 8, 9) // init by variadic parameter

    // push
    s1.push([4,5])    // array
    s2.push(10, 11)   // variadic
    s3.push(20)       // variadic

    // pop
    for _ in 1...4 { s1.pop() }
    s2.pop()
    s3.pop()

    // print these stacks
    example("stack struct", items:[s1,s2,s3])
}

func testStackClass_Var() {
    // init
    var s4: Stack = [1,2,3] // ⚠️ warning: s4 was never mutated; consider changing to let ...
    var s5 = Stack([4,5,6]) // init by array
    var s6 = Stack(7, 8, 9) // init by variadic parameter

    // push
    s4.push([4,5])    // array
    s5.push(10, 11)   // variadic
    s6.push(20)       // variadic

    // pop
    for _ in 1...4 { s4.pop() }

    // print these stacks
    example("stack class", items: [s4,s5,s6])
}

func testStackClass_Let() {
    // init
    let s7: Stack = [1,2,3] // expressible by array literal
    let s8 = Stack([4,5,6]) // init by array
    let s9 = Stack(7, 8, 9) // init by variadic parameter

    // push
    s7.push([4,5])    // array
    s8.push(10, 11)   // ⛔ Error: Extra argument in call
    s9.push(20)       // ⛔ Error: Cannot convert value of type 'Int' to expected argument type '[Int]'

    // pop
    for _ in 1...4 { s7.pop() }

    // print these stacks
    example("stack class", items: [s7,s8,s9])
}

我可以毫无问题地运行第一个测试用例testStackStruct():

testStackStruct()的输出

[1 ⇄ in/out
[4, 5, 6, 10 ⇄ in/out
[7, 8, 9 ⇄ in/out

并运行testStackClass_Var() 案例,只发出编译器警告:

⚠️ warning: s4 was never mutated; consider changing to let ...

testStackClass_Var()的输出

[1 ⇄ in/out
[4, 5, 6, 10, 11 ⇄ in/out
[7, 8, 9, 20 ⇄ in/out

但是testStackClass_Let()的情况甚至无法编译成功,我得到了两个编译器错误:

s8.push(10, 11)  // ⛔ Error: Extra argument in call
s9.push(20)      // ⛔ Error: Cannot convert value of type 'Int' to expected argument type '[Int]'

testStackClass_Var() case 和 testStackClass_Let() case 之间的唯一区别是我是使用 var 还是 let 来声明这些堆栈实例。

我不知道我在哪里或做错了什么,有人可以帮忙吗? 谢谢。

附言

我的小助手功能:

example.swift

import Foundation   // string.padding() needs this

// print example
// usage:
//    example("test something", items: [a,b,c]) {
//      // example code here ...
//    }
public func example<T>(
    _ title: String,         // example title
    length: Int      = 30,   // title length
    items: [T]?      = nil,  // items to print
    run: (()->Void)? = nil   // example code (optional)
){
    // print example title
    print(
        ("----- [ " + title + " ] ")
            .padding(toLength: length, withPad: "-", startingAt: 0)
    )

    // run example
    if let run = run { run() }

    // print items if provided
    if let items = items {
        items.forEach{ print($0) }
    }

    // new line
    print()
}

【问题讨论】:

  • 可以添加example的代码吗?
  • @Codo helper function example() 添加在上面。

标签: ios swift protocols


【解决方案1】:

你的

 public func push(_ newItems:[Item]) {

是public class Stack&lt;Item&gt;的一个方法,它是一个引用类型,所以你可以在一个常量上调用它:

let s4: Stack = [1,2,3]
s4.push([4,5])

另一方面,可变参数方法

public mutating func push(_ items:Item...)

是protocol CanStack的扩展方法,结构体也可以采用,所以需要一个变量。这就是为什么

let s8 = Stack([4,5,6]) // init by array
s8.push(10, 11)   // Error: Extra argument in call

不编译。

这是一个演示问题的简短示例:

protocol P {
    mutating func foo()
    mutating func bar()
}

extension P {
    mutating func bar() {}
}

class C: P {
    func foo() {}
}

let c = C()
c.foo()
c.bar() // Cannot use mutating member on immutable value: 'c' is a 'let' constant

根本原因(我从下面列出的来源中了解到)是在引用类型上调用的mutating func 不仅允许改变self 的属性,还允许替换 em> self 通过一个新值。

如果 P 被声明为 类协议(并且删除了 mutating 关键字),它将编译:

protocol P: class {
    func foo()
    func bar()
}

extension P {
    func bar() {}
}

相关资源:

【讨论】:

    【解决方案2】:

    但是 testStackClass_Let() 案例甚至无法编译成功,我有两个编译器 错误:

     s8.push(10, 11)  // ⛔ Error: Extra argument in call
    s9.push(20)      // ⛔ Error: Cannot convert value of type 'Int' to expected argument    type '[Int]'
    

    testStackClass_Var() case 和 testStackClass_Let() 案例是我使用 var 还是 let 来声明 那些堆栈实例。

    这就是在 Swift 结构上使用变异函数。 这是一个详细的答案: Thanks to Natasha-the-Robot

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2019-06-17
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2016-06-15
      • 2014-02-08
      相关资源
      最近更新 更多