无法将类型的值转换为预期的参数类型?

时间:2016-03-16 19:13:39

标签: swift function button ibaction for-in-loop

我正在尝试使用一个函数来设置不同按钮的属性。以下是发生错误的代码区域:

@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{}}}

调用setButton函数时发生错误。该错误突出显示该函数的ChemistryViewInfo部分。

错误是:

Cannot convert value of type "ChemistryViewInfo.Type" to expected argument type "ChemistryViewInfo".

这是我创建并正在调用的函数:

func setButton(viewInfo:ChemistryViewInfo,button:UIButton, index:Int) {
    let buttonInfo = viewInfo.buttons[index]
    button.titleLabel?.text = buttonInfo.scale
    button.backgroundColor = buttonInfo.color
}

我想知道这是因为我的代码中有错误还是我做错了。我有正确的按钮,所以我知道这不是问题。无论如何,请帮忙。

非常感谢任何建议或意见。请在您的答案中发布更改的代码。

提前致谢。

修改

以下是我要绘制的代码:

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." )]),

1 个答案:

答案 0 :(得分:0)

我假设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{}
        }
     }
}