xcode 错误:字符串类型的值没有成员 componentsSeparatedByCharactersInSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38210048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:00:41  来源:igfitidea点击:

Error: Value of type string has no member componentsSeparatedByCharactersInSet

iosxcodeswiftswift2swift3

提问by Cosmic Arrows

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

以下代码抛出以下错误:“字符串类型的值没有成员 componentsSeparatedByCharactersInSet”

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

此代码来自之前在 swift 版本 1 或 2 中工作但不再有效的另一个项目。

   import Foundation

extension String {

    func split() -> [String] {
        return self.componentsSeparatedByCharactersInSet(
            CharacterSet.whitespaceAndNewlineCharacterSet())
            .filter({
func split() -> [String] {
    return self.components(separatedBy: .whitespacesAndNewlines).filter{!##代码##.isEmpty}
}
!= ""}); } } 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; } }

回答by Alexander - Reinstate Monica

You're looking for components(separatedBy:):

您正在寻找components(separatedBy:)

##代码##