有没有办法在 Swift 中立即生成一个填充了一系列值的数组? [英] Is there a way to instantly generate an array filled with a range of values in Swift?

查看:44
本文介绍了有没有办法在 Swift 中立即生成一个填充了一系列值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在 python 中,我可以说一些类似

For example, in python I could say something along the lines of

arr = range(0,30) 

并获取包含所述元素的数组.我怀疑在 Swift 中使用下标可能会发生类似的事情,但是在搜索文档和 Apple 的 iBook 之后,我找不到用于生成所述数组的包含电池"的解决方案.

and get an array with said elements. I suspect that something similar might be possible with a subscript in Swift, but after scouring the documentation and Apple's iBook I can't find a "batteries-included" solution for generating said array.

这是我必须手动编写代码的东西,还是有预先编写的方法?

Is this something I'd have to write the code manually for, or is there a pre-written method for it?

推荐答案

您可以创建一个具有如下范围的数组:

You can create an array with a range like this:

var values = Array(0...100)

这给你一个 [0, ..., 100]

这篇关于有没有办法在 Swift 中立即生成一个填充了一系列值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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