Swift生成器上的链式表达错误 [英] Chain style expression error on swift generator

查看:53
本文介绍了Swift生成器上的链式表达错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迅速代表:

zip([1,2,3],[7,8,9]).generate().next() 
repl.swift:1:22: error: value of type 'Zip2Generator<IndexingGenerator<Array<Int>>, IndexingGenerator<Array<Int>>>' has no member 'next'

但这可行:

var gen = zip([1,2,3],[7,8,9]).generate()
gen.next()

为什么?

推荐答案

函数next()会使调用它的对象发生突变,因此必须将对象分配给var才能起作用.除非将zip([1,2,3],[7,8,9]).generate()分配给var,否则它是不可变的.

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.

为什么next()会使对象变异?因为有一个内部索引变量,用于跟踪接下来要显示的值. next()使用该内部索引,然后将其前进(对其进行突变)以指向下一项.

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.

这篇关于Swift生成器上的链式表达错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆