Swift 无法将类型的值转换为预期的参数类型?
我试图利用一个函数来设置不同按钮的属性。以下是发生错误的代码区域:Swift 无法将类型的值转换为预期的参数类型?,swift,function,button,ibaction,for-in-loop,Swift,Function,Button,Ibaction,For In Loop,我试图利用一个函数来设置不同按钮的属性。以下是发生错误的代码区域: @IBAction func pHTargetButton(sender: UIButton) { let buttonArray = [chemistryButton0!, chemistryButton1!, chemistryButton2!, chemistryButton3!, chemistryButton4!, chemistryButton5!] let chemistryViewChoice =
@IBAction func pHTargetButton(sender: UIButton) {
let buttonArray = [chemistryButton0!, chemistryButton1!, chemistryButton2!, chemistryButton3!, chemistryButton4!, chemistryButton5!]
let chemistryViewChoice = ["pH"]
for var i in 0..<buttonArray.count {
setButton(ChemistryViewInfo, button: buttonArray[i], i)
if buttonArray.count<5 {
chemistryButton5?.backgroundColor = UIColor(red: 255, green: 255, blue: 255)
chemistryButton5?.setTitle("", forState: UIControlState.Normal)
chemistryButton5?.alpha = 0.0} else{}}}
我想知道这是因为我的代码有错误还是我做了错事。我已经正确声明了所有按钮,因此我知道这不是问题所在。不管怎样,请帮忙
非常感谢您的任何建议或意见。请在您的答案中张贴更改后的代码
提前谢谢
编辑
以下是我从中提取的代码:
struct ChemistryButtonInfo{
let scale: String
let color: UIColor
let levelExamp: String
let levelInfo: String
}
struct ChemistryViewInfo {
let text: String
let description: String
let buttons: [ChemistryButtonInfo]
}
let chemistryViewChoice = ["pH":
ChemistryViewInfo(
text: "pH",
description: "Most living things depend on proper pH level to sustain life.",
buttons: [
ChemistryButtonInfo(scale: "6.2", color: UIColor(red: 212, green: 142, blue: 69), levelExamp: "Bread, Salmon, Potatoes, Normal Rain, Milk", levelInfo: "This level is not dangerous for humans. Only dangerous to some plant life."),
ChemistryButtonInfo(scale: "6.8", color: UIColor(red: 209, green: 122, blue: 31), levelExamp: "Milk and Human Saliva", levelInfo: "This level is not dangerous fr humans but on ly dangerouse to some plant life"),
ChemistryButtonInfo(scale: "7.2", color: UIColor(red: 224, green: 80, blue: 9), levelExamp: "Blood, Human Tears, and Pure Water", levelInfo: "This level is not dangerous for humans. Only dangerous to few species of plant life."),
ChemistryButtonInfo(scale: "7.8", color: UIColor(red: 194, green: 74, blue: 58), levelExamp: "Sea Water and Eggs", levelInfo: "This level is not dangerous for humans, plants or fish"),
ChemistryButtonInfo(scale: "8.4", color: UIColor(red: 208, green: 48, blue: 75), levelExamp: "Baking Soda and Phosphate Detergents", levelInfo: "This level is sometimes, in rare cases, dangerous for humans. Also dangerous to certain species of plant life and marine life." )]),
我假设ChemistryViewInfo是一个类名。您需要将ChemistryViewInfo的实例发送到setButton。比如:
@IBAction func pHTargetButton(sender: UIButton) {
let buttonArray = [chemistryButton0!, chemistryButton1!, chemistryButton2!, chemistryButton3!, chemistryButton4!, chemistryButton5!]
for var i in 0..<buttonArray.count {
setButton(chemistryViewChoice["pH"], button: buttonArray[i], i)
if buttonArray.count<5 {
chemistryButton5?.backgroundColor = UIColor(red: 255, green: 255, blue: 255)
chemistryButton5?.setTitle("", forState: UIControlState.Normal)
chemistryButton5?.alpha = 0.0} else{}
}
}
}
@iAction func pHTargetButton(发送方:UIButton){
让buttonArray=[chemistryButton0!、chemistryButton1!、chemistryButton2!、chemistryButton3!、chemistryButton4!、chemistryButton5!]
对于0..中的var i,我假设ChemistryViewInfo是一个类名。您需要将ChemistryViewInfo的实例发送到setButton。类似于:
@IBAction func pHTargetButton(sender: UIButton) {
let buttonArray = [chemistryButton0!, chemistryButton1!, chemistryButton2!, chemistryButton3!, chemistryButton4!, chemistryButton5!]
for var i in 0..<buttonArray.count {
setButton(chemistryViewChoice["pH"], button: buttonArray[i], i)
if buttonArray.count<5 {
chemistryButton5?.backgroundColor = UIColor(red: 255, green: 255, blue: 255)
chemistryButton5?.setTitle("", forState: UIControlState.Normal)
chemistryButton5?.alpha = 0.0} else{}
}
}
}
@iAction func pHTargetButton(发送方:UIButton){
让buttonArray=[chemistryButton0!、chemistryButton1!、chemistryButton2!、chemistryButton3!、chemistryButton4!、chemistryButton5!]
对于0中的var i。这会为每个按钮设置标题和背景色吗?@Bigfoot11使用chemistryViewChoice[“pH”]
但从函数中删除chemistryViewChoice。这会为每个按钮设置标题和背景色吗?@Bigfoot11使用chemistryViewChoice[“pH”]
但从函数中删除chemistryViewChoice。