component(separatedBy :)与.split(separator :)相关联

Sud*_*mar 8 swift

在Swift 4中,.split(separator:)apple在String struct中引入了新方法.所以要用一个更快的空格来分割一个字符串,例如.

let str = "My name is Sudhir"

str.components(separatedBy: " ")

//or 

str.split(separator: " ")
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 13

除了性能之外,它们处理空子序列的方式split(separator:)和方式 也存在重要差异components(separatedBy:)

如果您的输入包含尾随空格,它们将产生不同的结果

    let str = "My name is Sudhir " // trailing space

    str.split(separator: " ")
    // ["My", "name", "is", "Sudhir"]

    str.components(separatedBy: " ")
    // ["My", "name", "is", "Sudhir", ""] ? Additional empty string
Run Code Online (Sandbox Code Playgroud)

要使两者产生相同的结果,请使用omittingEmptySubsequences:false参数(默认为true):

    // To get the same behavior:
    str.split(separator: " ", omittingEmptySubsequences: false)
    // ["My", "name", "is", "Sudhir", ""]
Run Code Online (Sandbox Code Playgroud)

详情在这里:

https://developer.apple.com/documentation/swift/string/2894564-split


Sud*_*mar 5

我已经使用以下代码进行了样本测试.

  var str = """
                One of those refinements is to the String API, which has been made a lot easier to use (while also gaining power) in Swift 4. In past versions of Swift, the String API was often brought up as an example of how Swift sometimes goes too far in favoring correctness over ease of use, with its cumbersome way of handling characters and substrings. This week, let’s take a look at how it is to work with strings in Swift 4, and how we can take advantage of the new, improved API in various situations. Sometimes we have longer, static strings in our apps or scripts that span multiple lines. Before Swift 4, we had to do something like inline \n across the string, add an appendOnNewLine() method through an extension on String or - in the case of scripting - make multiple print() calls to add newlines to a long output. For example, here is how TestDrive’s printHelp() function (which is used to print usage instructions for the script) looks like in Swift 3  One of those refinements is to the String API, which has been made a lot easier to use (while also gaining power) in Swift 4. In past versions of Swift, the String API was often brought up as an example of how Swift sometimes goes too far in favoring correctness over ease of use, with its cumbersome way of handling characters and substrings. This week, let’s take a look at how it is to work with strings in Swift 4, and how we can take advantage of the new, improved API in various situations. Sometimes we have longer, static strings in our apps or scripts that span multiple lines. Before Swift 4, we had to do something like inline \n across the string, add an appendOnNewLine() method through an extension on String or - in the case of scripting - make multiple print() calls to add newlines to a long output. For example, here is how TestDrive’s printHelp() function (which is used to print usage instructions for the script) looks like in Swift 3
          """

    var newString = String()

    for _ in 1..<9999 {
        newString.append(str)
    }

    var methodStart = Date()

 _  = newString.components(separatedBy: " ")
    print("Execution time Separated By: \(Date().timeIntervalSince(methodStart))")

    methodStart = Date()
    _ = newString.split(separator: " ")
    print("Execution time Split By: \(Date().timeIntervalSince(methodStart))")
Run Code Online (Sandbox Code Playgroud)

我在iPhone6上运行上面的代码,结果如下

Execution time Separated By: 8.27463299036026 Execution time Split By: 4.06880903244019

结论:split(separator:)快于components(separatedBy:).

  • 如果您未启用完全优化,则无法进行比较. (4认同)
  • 然后请帮助我,我该如何测试。 (2认同)
  • 使用components返回一个[String],而split则返回一个[Substring]。每个`String`都是一个String分配,`Substring`仅使用引用,不分配新的String,并且更快,更便宜。 (2认同)