我不断收到此错误,不知道如何解决。谁能帮我吗?我要它相应地更新标题。
这里给我一个错误,countElement
,给我一个错误
使用未解析的标识符
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
}
}
}
}
答案 0 :(得分:1)
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...