刚刚升级到Xcode 8.3,现在出现错误:"类型GameScene没有成员:updateCounting"

时间:2017-04-01 00:00:39

标签: ios swift xcode swift3 xcode8

我的代码正在运行并且运行正常,直到我最近更新到Xcode 8.3。现在我的代码无法运行,给我留下了错误:" Type GameScene没有成员:updateCounting"。我已经阅读过类似的问题和解决方案,但没有人能够帮助解决这个问题。我仍在快速学习,所以任何帮助都会受到赞赏。

这是我的代码:

    let highScoreDefault = UserDefaults.standard
    highScore = highScoreDefault.integer(forKey: "Highscore")
    highScoreLabel.text = "\(highScore)"

func scoreUpdate(_ currentTime: CFTimeInterval){

    if (score > highScore) {
        highScore = score
        highScoreLabel.text = NSString(format: "Highscore : %i", highScore) as String

        let highscoreDefault = UserDefaults.standard
        highscoreDefault.setValue(highScore, forKey: "Highscore")
        highscoreDefault.synchronize()

    }

    }

func updateCounting() {
        scoreLabel.text = String(self.score)
        finalScore.text = String(self.score)

        let highScoreDefault = UserDefaults.standard
        highScore = highScoreDefault.integer(forKey: " ")
        highScoreLabel.text = "\(highScore)"

    if (score > highScore) {
        highScore = score
        highScoreLabel.text = NSString(format: " %i", highScore) as String

        let highscoreDefault = UserDefaults.standard
        highscoreDefault.setValue(highScore, forKey: " ")
        highscoreDefault.synchronize()
    }

        self.score += 1
        print(score, terminator: " ")

    }

        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(GameScene.updateCounting), userInfo: nil, repeats: true)

         // The above line of code is what's giving the error

        totalCoins = UserDefaults.standard.integer(forKey: "Total Coins")
        totalCoinLabel.text = "\(totalCoins)"

1 个答案:

答案 0 :(得分:0)

建议的解决方案

尝试在函数名之前添加@objc,如下所示:

@objc func updateCounting() {
    // function code here
}

然后使用#selector(self.updateCounting)

工作示例

我拿了你的代码并大大简化了它。我开始使用新的Single View项目。我添加了一个GameScene.swift文件并修改了ViewController类。见下面的代码。运行项目,您将看到日志显示:

Updating count: 0 
Updating count: 1 
Updating count: 2 
Updating count: 3

GameScene.swift

import Foundation

class GameScene {
    var timer: Timer = Timer()
    var counter: Int = 0        

    @objc func updateCounting(){
        print("Updating count: \(counter)")
        counter += 1
    }

    func updateTimer() {
        timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateCounting), userInfo: nil, repeats: true)
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let game = GameScene()
        game.updateTimer()
    }
}

Github链接: https://github.com/starkindustries/TimerTestProject

有效的语法选项

在此示例项目中成功测试了以下选项:

  1. selector: #selector(updateCounting)
  2. selector: #selector(self.updateCounting)
  3. selector: #selector(GameScene.updateCounting)
  4. 假设您要为updateCounting函数添加参数。将您的updateCounting方法名称更改为@objc func updateCounting(_ timer: Timer),并将您的选择器更改为selector: #selector(updateCounting(_:)

    语法讨论

    一旦删除@objc,编译器就会显示以下错误:

      

    '#selector'的参数是指实例方法'updateCounting'   没有暴露于Objective-C

    另一个答案中的一条评论也暗示了这一点:

      

    我需要像这样添加写目标函数:@objc func   更新时间() {}   Timer.scheduledTimer swift 3 pre iOS 10 compatibility

    Apple文档声明:

      

    选择器应具有以下签名:timer Fire Method:   (包括冒号以指示该方法接受参数)。该   timer将自身作为参数传递,因此该方法将采用   以下模式:    - (void)timerFireMethod:(NSTimer *)timer https://developer.apple.com/reference/foundation/timer/1412416-scheduledtimer

    即使我在Swift版本的文档中,它也显示了timerFireMethod的Objective C版本。因此,Timer类可能需要一个Objective C选择器,就像抱怨的编译器错误一样。但是,文档说明该方法应该有一个参数(计时器),但我只是使用没有参数的函数成功测试了它。