【问题标题】:Just Upgraded To Xcode 8.3, now getting an error: "Type GameScene has no member: updateCounting"刚刚升级到 Xcode 8.3,现在出现错误:“Type GameScene has no member: updateCounting”
【发布时间】:2017-04-01 00:00:39
【问题描述】:

我的代码运行正常,直到我最近更新到 Xcode 8.3。现在我的代码无法运行,给我留下错误:“类型 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)"

【问题讨论】:

  • 与您的问题无关,但不要使用setValue(_:forKey:) 将数据保存到UserDefaults。使用set(_:forKey:)。并且无需拨打synchronize
  • 你的函数scoreUpdateupdateCounting存储的类叫GameScene?
  • 为什么不直接调用updateCounting而不是加上它的类名GameScene
  • 感谢@rmaddy 的提示!
  • @paper1111 是的!那是我的函数当前存储的地方。

标签: ios swift xcode swift3 xcode8


【解决方案1】:

建议的解决方案

尝试在函数名称前添加@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)

假设您想向 updateCounting 函数添加一个参数。将您的 updateCounting 方法名称更改为 @objc func updateCounting(_ timer: Timer) 并将您的选择器更改为 selector: #selector(updateCounting(_:)

语法讨论

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

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

另一个答案中的一个 cmets 也暗示了这一点:

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

Apple 文档指出:

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

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

【讨论】:

  • 使用 selector: #selector(updateCounting) 导致错误提示:#selector 的参数不能引用本地函数 updateCounting()。第二个建议导致相同的错误消息。
  • 尝试在你的函数名前添加@objc。请参阅我的更新答案。让我知道。谢谢
  • 我还不能解决这个错误。当我输入#selector(insert function here) 时,它无法找到我的任何函数,包括updateCounting。我相信问题可能出在我最近组织我的代码以使其看起来更整洁并将相关的代码部分放在一起时。错误没有立即出现。我认为“计时器”代码行在重新排列后可能位于错误的位置,所以我尝试将其移动到不同的位置,但没有成功。我不知道为什么 GameScene 目前无法找到我的任何功能。我整理的代码:
  • override func didMove(to view: SKView) { 函数中
  • 我建议您在 GitHub 上提供您的代码的链接,以便其他人可以看到可能发生的其他情况。
猜你喜欢
  • 1970-01-01
  • 2016-12-11
  • 2018-08-14
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
相关资源
最近更新 更多