连接Swift的Intift数组来创建一个新的Int [英] Concatenate Swift Array of Int to create a new Int

查看:193
本文介绍了连接Swift的Intift数组来创建一个新的Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数组< Int> [1,2,3,4] )转换成常规 Int 1234 )?我可以让它走另一条路(将 Int 分成单个数字),但我无法弄清楚如何组合数组以便数字组成新数字的数字。

How can you make an Array<Int> ([1,2,3,4]) into a regular Int (1234)? I can get it to go the other way (splitting up an Int into individual digits), but I can't figure out how to combine the array so that the numbers make up the digits of a new number.

推荐答案

这将有效:

let digits = [1,2,3,4]
let intValue = digits.reduce(0, combine: {$0*10 + $1})






注意

这个答案假设输入数组中包含的所有Ints都是数字 - 0 ... 9。除此之外,例如,如果您想将 [1,2,3,4,56] 转换为Int 123456 ,你需要其他方式。

This answer assumes all the Ints contained in the input array are digits -- 0...9 . Other than that, for example, if you want to convert [1,2,3,4, 56] to an Int 123456, you need other ways.

这篇关于连接Swift的Intift数组来创建一个新的Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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