swift - I get this error " Use of unresolved identifier 'countElement'". I am new to swift, so I cant figure whats wrong
问题描述
I keep getting this error and don't know how to solve it. Can anyone help me out? The thing is I want it to update the title accordingly.
Here it gives me an error at countElement
, where it gives me an error
Use of unresolved identifier
for item in components {
if countElement(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespacesAndNewlines())) > 0
{
self.navigationItem.title = item
break
}
import UIKit
//the protocol (or delegate) pattern, so we can update the table view's selected item
protocol NoteViewDelegate {
//the name of the function that will be implemented
func didUpdateNoteWithTitle(newTitle : String, andBody newBody :
String)
}
class NotesViewController: UIViewController , UITextViewDelegate {
//a variable to hold the delegate (so we can update the table view)
var delegate : NoteViewDelegate?
//a variable to link the Done button
@IBOutlet weak var btnDoneEditing: UIBarButtonItem!
@IBOutlet weak var txtBody : UITextView!
//a string variable to hold the body text
var strBodyText : String!
override func viewDidLoad() {
super.viewDidLoad()
self.txtBody.becomeFirstResponder()
//allows UITextView methods to be called (so we know when they begin editing again)
self.txtBody.delegate = self
//set the body's text to the intermitent string
self.txtBody.text = self.strBodyText
//makes the keyboard appear immediately
self.txtBody.becomeFirstResponder()
}
@IBAction func doneEditingBody() {
//tell the main view controller that we're going to update the selected item
//but only if the delegate is NOT nil
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle( newTitle: self.navigationItem.title!, andBody: self.txtBody.text)
}
//hides the keyboard
self.txtBody.resignFirstResponder()
//makes the button invisible (still allowed to be pressed, but that's okay for this app)
self.btnDoneEditing.tintColor = UIColor.clear
}
func textViewDidBeginEditing(_ textView: UITextView) {
//sets the color of the Done button to the default blue
//it's not a pre-defined value like clearColor, so we give it the exact RGB values
self.btnDoneEditing.tintColor = UIColor(red: 0, green:
122.0/255.0, blue: 1, alpha: 1)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//tell the main view controller that we're going to update the selected item
//but only if the delegate is NOT nil
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle(newTitle: self.navigationItem.title!, andBody: self.txtBody.text)
}
}
func textViewDidChange(_ textView: UITextView) {
let components = self.txtBody.text.components(separatedBy: "\n")
self.navigationItem.title = ""
for item in components {
if countElement(item.stringByTrimmingCharactersInSet(NSCharacterSet.whitespacesAndNewlines())) > 0 {
self.navigationItem.title = item
break
}
}
}
}
解决方案
countElement
是一种非常古老的语法,已经被替换count
了很长时间。
当前(优化的)Swift 4 语法是
func textViewDidChange(_ textView: UITextView) {
let components = self.txtBody.text.components(separatedBy: "\n")
self.navigationItem.title = components.first{ !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } ?? ""
}
永远不要使用 . 检查空字符串或空集合类型.count == 0
。有isEmpty
。
并且永远不要使用像这样可怕的语法
if self.delegate != nil {
self.delegate!.didUpdateNoteWithTitle...
这是 Swift,有可选链接
self.delegate?.didUpdateNoteWithTitle...
推荐阅读
- python - 如何在 Python 3 中处理多个异常?
- typescript - 用户生成对象的打字稿类型
- javascript - 试图理解异步等待
- java - 如果满足 if 循环内部条件,如何退出 for 循环
- c# - Is there a way to initialize EII property within constructor?
- html - 使用没有后端的 gmail 的 HTML 表单?
- flutter - 如何使用条件语句颤动在shared_preferences中保存查询中的值
- angular - 在路由模块内以编程方式更改路由数组
- r - 将 Rmarkdown 文档编织为 html/pdf 失败
- flutter - 如何用颤振对json文件上相同日期的不同数据进行分组?