gpt4 book ai didi

ios - 错误?读取 IBAction 按钮内的变量

转载 作者:行者123 更新时间:2023-11-30 12:47:35 24 4
gpt4 key购买 nike

我有一个 IBAction 需要从变量中读取。但是,当将变量放置在 viewDidLoad 之外时,我收到错误

Cannot use instance member 'animal' within property initializer; property initializers run before 'self' is available

我的代码如下。当我将变量移回 viewDidLoad 内部时,错误表明animalName 未声明

var animalName = (String(format: "%03d", animal.speciesId!))

@IBAction func megaKeyIBO(_ sender: Any) {
animalName = (String(format: "%03d", animal.speciesId!)) + "-merg"
}

override func viewDidLoad() {
super.viewDidLoad()

//variable was originally here before moving to top
var animalName = (String(format: "%03d", animal.speciesId!))
}

最佳答案

发生这种情况是因为您尝试在属性animal.speciesId之前初始化animalName!已初始化。一个简单的解决方法是这样的:

var animalName: String!

@IBAction func megaKeyIBO(_ sender: Any) {
animalName = (String(format: "%03d", animal.speciesId!)) + "-merg"
}

override func viewDidLoad() {
super.viewDidLoad()

// Initialize on viewDidLoad
animalName = (String(format: "%03d", animal.speciesId!))
}

关于ios - 错误?读取 IBAction 按钮内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41433042/

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