[英]Unable to convert [Int] to integer in swift
I am having 5 arrayS. 我有5个arrayS。 I removed duplicates from array.
我从数组中删除了重复项。 But, I unable to store new values in new array.
但是,我无法在新数组中存储新值。 I following the below method to convert in run time.
我按照以下方法在运行时进行转换。 The below logics will occur inside the loop.
以下逻辑将在循环内发生。 But, I showed my logic very detaily.
但是,我的逻辑很详细。 I unable to convert integer array to int.
我无法将整数数组转换为int。 So I tried to convert integer array to String.
所以我试图将整数数组转换为String。 But failed.
但是失败了。 My coding is below.
我的编码如下。
var arr_1 = [Int]()
var arr_2 = [Int]()
var arr_3 = [Int]()
var arr_4 = [Int]()
var arr_5 = [Int]()
var arr_final = [Int]()
arr_1 = [2,2,2,2,2,2]
arr_2 = [3,3,3]
arr_3 = [5,5,5,5,5]
arr_4 = [1,1,1,1]
arr_5 = [0]
My Output to be: 我的输出是:
arr_final = [2,3,5,1,0]
My Coding 我的编码
var str_1 = String(stringInterpolationSegment: uniq(arr_1)).toInt()
arr_final.append(str_1!) //PRINTS FATAL ERROR
//REMOVE DUPLICATES
func uniq<S: SequenceType, E: Hashable where E==S.Generator.Element>(source: S) -> [E] {
var seen: [E:Bool] = [:]
return filter(source) { seen.updateValue(true, forKey: $0) == nil }
}
The function uniq
returns always an array of Int
. 函数
uniq
始终返回一个Int
数组。
To append an Array
to an existing Array
use extend
rather than append
要将
Array
附加到现有Array
使用extend
而不是append
var arr_final = [Int]()
let arr_1 = [2,2,2,2,2,2]
let result = uniq(arr_1)
arr_final.extend(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.