Swift 中的计数元素 3
countElements in Swift 3
swift3这部分代码怎么写?我正在构建一个笔记应用程序,这部分是关于将在 tableView 单元格中显示的标题
if countElements(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) > 0
func textViewDidChange(textView: UITextView) {
//separate the body into multiple sections
let components = self.txtBody.text.componentsSeparatedByString("\n")
//reset the title to blank (in case there are no components with valid text)
self.navigationItem.title = ""
//loop through each item in the components array (each item is auto-detected as a String)
for item in components {
//if the number of letters in the item (AFTER getting rid of extra white space) is greater than 0...
if countElements(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) > 0 {
//then set the title to the item itself, and break out of the for loop
self.navigationItem.title = item
break
}
}
}
这应该可以解决您的问题:
if item.trimmingCharacters(in: .whitespacesAndNewlines).characters.count > 0 {}
这个怎么样?
extension String {
func firstNonEmptyLine() -> String? {
let lines = components(separatedBy: .newlines)
return lines.first(where: {
![=10=].trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
}
}
func textViewDidChange(textView: UITextView) {
self.navigationItem.title = self.txtBody.text.firstNonEmptyLine() ?? "Default title if there's no non-empty line"
}
查询是否存在不属于给定字符集的字符:使用正确的工具完成工作
// if the number of letters in the item (AFTER getting
// rid of extra white space) is greater than 0...
如果您只是想知道 String
实例 item
中是否有任何字符 不是 在 CharacterSet.whitespacesAndNewlines
中(a unicode 标量集),没有理由使用 trimmingCharacters(in:)
方法,而是使用 short-circuit 方法来查找第一个可能不在此字符集中的 unicode 标量
if item.unicodeScalars.contains(where:
{ !CharacterSet.whitespacesAndNewlines.contains([=11=]) }) {
// ...
}
或者,利用(也 short-circuiting)rangeOfCharacter(from:options:range:)
来查找是否可以找到任何属于反转 .whitespaceAndNewlines
字符集的字符
if item.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines.inverted) != nil {
// ...
}
swift3这部分代码怎么写?我正在构建一个笔记应用程序,这部分是关于将在 tableView 单元格中显示的标题
if countElements(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) > 0
func textViewDidChange(textView: UITextView) {
//separate the body into multiple sections
let components = self.txtBody.text.componentsSeparatedByString("\n")
//reset the title to blank (in case there are no components with valid text)
self.navigationItem.title = ""
//loop through each item in the components array (each item is auto-detected as a String)
for item in components {
//if the number of letters in the item (AFTER getting rid of extra white space) is greater than 0...
if countElements(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())) > 0 {
//then set the title to the item itself, and break out of the for loop
self.navigationItem.title = item
break
}
}
}
这应该可以解决您的问题:
if item.trimmingCharacters(in: .whitespacesAndNewlines).characters.count > 0 {}
这个怎么样?
extension String {
func firstNonEmptyLine() -> String? {
let lines = components(separatedBy: .newlines)
return lines.first(where: {
![=10=].trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
}
}
func textViewDidChange(textView: UITextView) {
self.navigationItem.title = self.txtBody.text.firstNonEmptyLine() ?? "Default title if there's no non-empty line"
}
查询是否存在不属于给定字符集的字符:使用正确的工具完成工作
// if the number of letters in the item (AFTER getting // rid of extra white space) is greater than 0...
如果您只是想知道 String
实例 item
中是否有任何字符 不是 在 CharacterSet.whitespacesAndNewlines
中(a unicode 标量集),没有理由使用 trimmingCharacters(in:)
方法,而是使用 short-circuit 方法来查找第一个可能不在此字符集中的 unicode 标量
if item.unicodeScalars.contains(where:
{ !CharacterSet.whitespacesAndNewlines.contains([=11=]) }) {
// ...
}
或者,利用(也 short-circuiting)rangeOfCharacter(from:options:range:)
来查找是否可以找到任何属于反转 .whitespaceAndNewlines
字符集的字符
if item.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines.inverted) != nil {
// ...
}