在Swift 4中,新方法.split(separator:)
由Apple在String結構中引入。因此,爲了與空白這是e.g更快分割字符串..組件(separateBy :)與.split(分隔符:)
let str = "My name is Sudhir"
str.components(separatedBy: " ")
//or
str.split(separator: " ")
在Swift 4中,新方法.split(separator:)
由Apple在String結構中引入。因此,爲了與空白這是e.g更快分割字符串..組件(separateBy :)與.split(分隔符:)
let str = "My name is Sudhir"
str.components(separatedBy: " ")
//or
str.split(separator: " ")
我已經樣本測試與下面的代碼。
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))")
我運行上方iPhone6代碼,下面是結果
Execution time Separated By: 8.27463299036026 Execution time Split By: 4.06880903244019
結論:split(separator:)
比components(separatedBy:)
更快。
我不認爲這有任何性能差異。 – dasdom
爲什麼不測試它並測量性能? - 順便說一句,這個方法在Swift 4中並不新鮮,請參閱https://stackoverflow.com/a/25229901/1187415。 –
@MartinR請參閱https://developer.apple.com/documentation/swift/string?changes=latest_minor蘋果提到它在最新版本中添加的內容 –