如何将数组转换为元组? [英] How can I convert an array to a tuple?

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

问题描述

我只想在 Swift 中将数组转换为元组;类似于以下内容:

<预><代码>>>>myArray = [1,2,3,4,5]>>>mytuple = 元组(myArray)>>>元组(1, 2, 3, 4, 5)

最简单的方法是什么?

(我今天才开始学习 Swift,所以我非常非常新).

解决方案

您不能这样做,因为元组的大小是其类型的一部分,而对于数组则不然.如果您知道数组的长度,您只需执行 let myTuple = (myArray[0], myArray[1], ...).或者您可以从一开始就围绕元组构建您的架构.你想做什么?

这是在许多其他语言中都会出现的常见问题.请参阅 如何将列表转换为元组在 Haskell 中?

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).

解决方案

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?

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天全站免登陆