Mr.KLD Mr.KLD - 1 year ago 105
iOS Question

Convert Swift string to array

How can I convert a string "Hello" to an array ["H","e","l","l","o"] in Swift?

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];

Answer Source

It is even easier in Swift:

let string : String = "Hello