我一直收到这个错误,但不知道如何解决。谁能帮我吗?问题是我希望它相应地更新标题。
这里它在 countElement
处给我一个错误,在那里它给我一个错误
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,有Optional Chaining
self.delegate?.didUpdateNoteWithTitle...
关于swift - 我收到此错误 "Use of unresolved identifier ' countElement'”。我是 swift 的新手,所以我不知道出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347118/