将Swift字符串转换为数组 [英] Convert Swift string to array
本文介绍了将Swift字符串转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Swift中将字符串Hello转换为数组[H,e,l,l,o]?
How can I convert a string "Hello" to an array ["H","e","l","l","o"] in Swift?
在Objective-C中我使用过:
In Objective-C I have used this:
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
推荐答案
在Swift中更容易:
It is even easier in Swift:
这篇关于将Swift字符串转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文