【问题标题】:Cannot convert value of type to expected argument type?无法将类型的值转换为预期的参数类型?
【发布时间】:2016-07-02 21:08:14
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我假设 ChemistryViewInfo 是一个类名。您需要向 setButton 发送一个 ChemistryViewInfo 实例。比如:

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

    【讨论】:

    • 这会为每个按钮设置标题和背景颜色吗?
    • @Bigfoot11 使用 chemistryViewChoice["pH"] 但从函数中删除 chemistryViewChoice。
    猜你喜欢
    • 2016-07-27
    • 2016-03-21
    • 1970-01-01
    • 2020-03-01
    • 2016-08-01
    • 2017-02-07
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多