swift - 我收到此错误 "Use of unresolved identifier ' countElement'”。我是 swift 的新手,所以我不知道出了什么问题

标签 swift xcode

我一直收到这个错误,但不知道如何解决。谁能帮我吗?问题是我希望它相应地更新标题。 这里它在 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/

相关文章:

swift - iOS 模拟器需要 1 个多小时才能运行一个应用程序

ios - UIButton 处于循环中,事件仅在第一个元素上触发

ios - 快速从 Backendless 中获取图像文件

xcode - 将我的 Swift 代码转换为 Swift 2.0 问题

objective-c - PhoneGap 如何从命令行进行发布构建

ios - 想要将 UIAlert 移动到 View 的顶部

ios - Popover View Controller 的角半径

xcode - 命令行上的 MacOS 公证无法创建身份验证 session

iPhone 类似谷歌地图应用的半透明状态栏

cocoa - 如何在 Cocoa 应用程序中包含 OpenCV?