user6510422 user6510422 - 4 months ago 63
iOS Question

Error: Value of type string has no member componentsSeparatedByCharactersInSet

The following code throws the following error: "Value of type string has no member componentsSeparatedByCharactersInSet"

This code is from another project that worked before in swift versions 1 or 2 but no longer works.

import Foundation

extension String {

func split() -> [String] {
return self.componentsSeparatedByCharactersInSet(
CharacterSet.whitespaceAndNewlineCharacterSet())
.filter({$0 != ""});
}
}

extension Array {

func unique<T: Equatable>() -> [T] {
var uniqueValues = [T]();

for value in self {
if !contains(uniqueValues, value as T) {
uniqueValues.append(value as! T);
}
}
return uniqueValues;
}

func first<T>(test:(T) -> Bool) -> T? {
for value in self {
if test(value as! T) {
return value as? T;
}
}
return nil;
}
}

Answer

You're looking for components(separatedBy:):

func split() -> [String] {
    return self.components(separatedBy:
        CharacterSet.whitespaceAndNewlineCharacterSet())
        .filter{$0 != ""};
}
Comments