转换字符串数组说明数组 [英] Convert string array description to array

查看:134
本文介绍了转换字符串数组说明数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

var data = [12,33,22,44];
var a =  data.description;  // results is "[12,33,22,44]"
var new = Array(a); // result is ['[','1'.  not [12,33,22,44]

除了分裂'a'和迭代的结果,还有就是一个字符串转换为数组的最快方法?

Aside splitting 'a' and iterating the result, There is a fastest way to convert a string to an array ?

感谢

推荐答案

您可以做到这一点的分裂阵列和筛选,不能转换为内部所有元素。

You can do that by splitting array and filtering all elements that cannot be converted to Int.

 "[12,33,22,44]".componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "[,]")).filter{ $0.toInt() != nil }.map{ $0.toInt()! } // [12, 33, 22, 44]

这篇关于转换字符串数组说明数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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