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;
}
}
You're looking for components(separatedBy:)
:
func split() -> [String] {
return self.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With