gpt4 book ai didi

ios - 刚刚升级到 Xcode 8.3,现在出现错误 : "Type GameScene has no member: updateCounting"

转载 作者:行者123 更新时间:2023-11-30 12:33:31 25 4
gpt4 key购买 nike

我的代码一直在运行并正常运行,直到我最近更新到 Xcode 8.3。现在我的代码无法运行,出现错误:“Type GameScene has no member: 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)"

最佳答案

建议的解决方案

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

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

然后使用#selector(self.updateCounting)

工作示例

我采用了您的代码并对其进行了大幅简化。我从一个新的单一 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(updateCounting)
  2. 选择器:#selector(self.updateCounting)
  3. 选择器:#selector(GameScene.updateCounting)

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

语法讨论

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

Argument of '#selector' refers to instance method 'updateCounting' that is not exposed to Objective-C

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

I needed to add write the target function like this: @objc func updateTime() {} Timer.scheduledTimer swift 3 pre iOS 10 compatibility

Apple 文档指出:

The selector should have the following signature: timer​Fire​Method:​ (including a colon to indicate that the method takes an argument). The timer passes itself as the argument, thus the method would adopt the following pattern: - (void)timerFireMethod:(NSTimer *)timer https://developer.apple.com/reference/foundation/timer/1412416-scheduledtimer

即使我使用的是 Swift 版本的文档,它也显示了 Objective C 版本的timerFireMethod。因此,也许 Timer 类需要一个 Objective C 选择器,就像所提示的编译器错误一样。然而,文档指出该方法应该有一个参数(计时器),但我只是使用不带参数的函数成功测试了它。

关于ios - 刚刚升级到 Xcode 8.3,现在出现错误 : "Type GameScene has no member: updateCounting",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com