字符串类型的值没有成员

字符串类型的值没有成员

本文介绍了错误:字符串类型的值没有成员组件SeparatedByCharactersInSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码引发以下错误:字符串类型的值没有成员componentSeparatedByCharactersInSet"

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

此代码来自另一个项目,该项目以前在swift版本1或2中起作用,但不再起作用.

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

推荐答案

您正在寻找 components(separatedBy:) :

You're looking for components(separatedBy:):

func split() -> [String] {
    return self.components(separatedBy: .whitespacesAndNewlines).filter{!$0.isEmpty}
}

这篇关于错误:字符串类型的值没有成员组件SeparatedByCharactersInSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:50