Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 807
Laurence Wingo Avatar asked Dec 18 '22 15:12

Laurence Wingo


1 Answers

You're looking for components(separatedBy:):

func split() -> [String] {
    return self.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
}
like image 158
Alexander Avatar answered May 21 '23 16:05

Alexander