简体   繁体   中英

Swift Value of type “ViewController” has no member named “Self” error

I am trying to understand trailing edge closures in Swift, and this code gives me an error in the promptForAnswer() . Specifically, I am getting an Xcode error which says:

Value of type "ViewController" has no member named "Self"

...even though I have declared it in the code.

Can someone explain what is wrong?

import UIKit
import GameplayKit

var allWords = [String]()
var usedWords = [String]()

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(promptForAnswer))

        if let startWordsPath = Bundle.main.path(forResource: "start", ofType: "txt") {
            if let startWords = try? String(contentsOfFile: startWordsPath) {
                allWords = startWords.components(separatedBy: "\n")
            }
        } else {
            allWords = ["silkworm"]
        }
        startGame()
    }

    @objc func promptForAnswer() {
        let ac = UIAlertController(title: "Enter answer", message: nil, preferredStyle: .alert)
        ac.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [unowned self, ac] (action: UIAlertAction) in
            let answer = ac.textFields![0]
            self.submit(answer: answer.text!) // Value of type "ViewController" has no member type "self"
        }

        ac.addAction(submitAction)
        present(ac, animated: true)
    }

    func startGame() {
        allWords = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: allWords) as! [String]
        title = allWords[0]
        usedWords.removeAll(keepingCapacity: true)
        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return usedWords.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Word", for: indexPath)
        cell.textLabel?.text = usedWords[indexPath.row]
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

When I pasted your code into a playground in Xcode 9.2 using the default toolchain (Swift 4.0), I got this error on the line you indicated:

Value of type 'ViewController' has no member 'submit'

You can resolve this error by adding a method to your controller that looks like this:

func submit(answer: String) {
  // ...
}

Maybe there was a bug in whatever compiler version you were using. Or maybe it was just having a bad day. Sometimes weird things happen. ¯\\_(ツ)_/¯

Things can change a lot between even minor versions of Swift, so make sure to include the versions of Xcode and the toolchain you are using. It makes reproducing your experience significantly easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM