首页 > 解决方案 > 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
        }

    }
    }

}

标签: swiftxcode

解决方案


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...

推荐阅读