Chain style expression error on swift generator

后端 未结 1 1576
清酒与你
清酒与你 2021-01-16 07:16

swift repl:

zip([1,2,3],[7,8,9]).generate().next() 
repl.swift:1:22: error: value of type \'Zip2Generator>, Index         


        
相关标签:
1条回答
  • 2021-01-16 07:37

    The function next() mutates the object that it is called on, so that object must be assigned to a var for it to work. zip([1,2,3],[7,8,9]).generate() is immutable until you assign it to a var.

    Why does next() mutate the object? Because there is an internal index variable that is keeping track of which value to show next. next() uses that internal index and then advances it (mutates it) to point to the next item.

    0 讨论(0)
提交回复
热议问题