This question already has answers here:
Split a String into an array in Swift?
(33个答案)
三年前关闭。
我想把下面的字符串转换成NSMuableArray。
var components: String = "library/book/science"

例如,我想在Swift中执行以下目标c代码
NSArray *componentsArray = [@"library/book/science" componentsSeparatedByString:@"/"];

NSMutableArray *componentsMutableArray = [[NSMutableArray alloc] initWithArray:componentsArray];
[componentsMutableArray addObject:@"astronomy"];

最佳答案

这是您的工作swift代码:

var components: String = "library/bool/science"

let componentsArray = components.componentsSeparatedByString("/")     //["library", "bool", "science"]

let componentsMutableArray = NSMutableArray(array: componentsArray)   //["library", "bool", "science"]

componentsMutableArray.addObject("astronomy")                         //["library", "bool", "science", "astronomy"]

08-06 14:06