我怎么能转换成数组元组? [英] How can I convert an array to a tuple?

查看:92
本文介绍了我怎么能转换成数组元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想一个数组转换成在斯威夫特的元组;类似如下:

I just want to convert an array into a tuple in Swift; something like the following:

>>> myArray = [1,2,3,4,5]
>>> mytuple = tuple(myArray)
>>> mytuple
(1, 2, 3, 4, 5)

什么是这样做的最简单的方法?

What's the easiest way of doing that?

(我才开始学习今天斯威夫特,让我非常非常新)

(I only started learning Swift today, so I am very, very new).

推荐答案

您不能这样做,因为一个元组的大小是其类型的一部分​​,这是不正确的数组。如果您知道数组的长度,你可以做让myTuple =(myarray的[0],myArray的[1],...)。或者你也可以建立你周围的建筑从一开始的元组。什么是你想怎么办?

You can't do this because the size of a tuple is part of its type, and this isn't true for an array. If you know the array's length, you can just do let myTuple = (myArray[0], myArray[1], ...). Or you can build your architecture around tuples from the beginning. What are you trying to do?

这是发生在许多其他语言的一个共同的问题。见如何将列表转换为一个元组在Haskell?

This is a common problem which occurs in lots of other languages. See How do I convert a list to a tuple in Haskell?

这篇关于我怎么能转换成数组元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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