1

I know the unresolved identifier is usually a pretty easy fix in Swift but I am still experiancing some difficulty anyways. I know I am not declaring the instance or variable I need to but I am unsure of the correct way to do so. Here is my code:

import UIKit
import Foundation

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


class ChemistryMasterViewController: UIViewController {

    @IBOutlet weak var chemistryButton0: UIButton!
    @IBOutlet weak var chemistryButton1: UIButton!
    @IBOutlet weak var chemistryButton2: UIButton!
    @IBOutlet weak var chemistryButton3: UIButton!
    @IBOutlet weak var chemistryButton4: UIButton!
    @IBOutlet weak var chemistryButton5: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let viewInfo = viewInfo {
            setButton(viewInfo, button: &chemistryButton0, index: 0)
            setButton(viewInfo, button: &chemistryButton1, index: 1)
            setButton(viewInfo, button: &chemistryButton2, index: 2)
            setButton(viewInfo, button: &chemistryButton3, index: 3)
            setButton(viewInfo, button: &chemistryButton4, index: 4)
            setButton(viewInfo, button: &chemistryButton5, index: 5)
        }
}

}

And...

import Foundation

import UIKit

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)),
        ChemistryButtonInfo(scale: "6.8", color: UIColor(red: 209, green: 122, blue: 31)),
        ChemistryButtonInfo(scale: "7.2", color: UIColor(red: 196, green:  80, blue:  9)),
        ChemistryButtonInfo(scale: "7.8", color: UIColor(red: 194, green:  74, blue: 58)),
        ChemistryButtonInfo(scale: "8.4", color: UIColor(red: 208, green:  48, blue: 75))]),
"Ammonia":
    ChemistryViewInfo(
        text: "Ammonia",
        description: "",

        buttons: [
            ChemistryButtonInfo(scale: "0.00", color: UIColor(red: 244, green: 235, blue: 130)),
            ChemistryButtonInfo(scale: "0.25", color: UIColor(red: 233, green: 233, blue: 156)),
            ChemistryButtonInfo(scale: "0.50", color: UIColor(red: 223, green: 238, blue: 141)),
            ChemistryButtonInfo(scale: "1.00", color: UIColor(red: 221, green: 236, blue: 210)),
            ChemistryButtonInfo(scale: "3.00", color: UIColor(red: 202, green: 227, blue: 191)),
            ChemistryButtonInfo(scale: "6.00", color: UIColor(red: 202, green: 216, blue: 173))])
]

Lastly:

import Foundation
import UIKit

struct ChemistryButtonInfo{
    let scale: String
    let color: UIColor
}

struct ChemistryViewInfo {

    let text: String
    let description: String
    let buttons: [ChemistryButtonInfo]
    }

    extension UIColor {
    convenience init(red:Int, green:Int, blue:Int) {
        self.init(
            red:   CGFloat(red)/255.0,
            green: CGFloat(green)/255.0,
            blue:  CGFloat(blue)/255.0,
            alpha: 1.0)
    }
}

The error is on the second viewInfo on the if-let statement. I am wondering what the correct way is to declare viewInfo.

Any suggestion, input, or answers are greatly appreciated.

Thanks in advance.

Daniel
  • 20,420
  • 10
  • 92
  • 149
Bigfoot11
  • 911
  • 2
  • 11
  • 25
  • Using an array of IBOutlets would simplify your code a lot. See [how to use them here](http://stackoverflow.com/a/24805344/386738) – Daniel Feb 04 '16 at 14:23
  • your class `ChemistryMasterViewController` must contain a `var viewInfo: ChemistryViewInfo?` instance variable if you are going to use that `if let` statement – Daniel Feb 04 '16 at 14:28
  • I did that but the error I commented below on @Predeep K's answer is what came up on all the lines within the if-let statement – Bigfoot11 Feb 04 '16 at 14:29
  • by doing `if let viewInfo = viewInfo {...}`you are saying you want a local variable `viewInfo` (which would be of type `ChemistryViewInfo`) made with the content of the currently existing variable `viewInfo` which must be of type `ChemistryViewInfo?` (note the `?`), but only if it is not `nil` – Daniel Feb 04 '16 at 14:31
  • I am using this code: `var viewInfo: ChemistryViewInfo?`. But the error is still there. – Bigfoot11 Feb 04 '16 at 14:35
  • The button title should be set as: `button.setTitle(buttonInfo.scale, forState: UIControlState.Normal)` – Daniel Feb 04 '16 at 14:41
  • Instead of this line?: `button.titleLabel?.text = buttonInfo.scale` – Bigfoot11 Feb 04 '16 at 14:44
  • Yes, since that code would crash if for some reason titleLabel is nil – Daniel Feb 04 '16 at 14:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102608/discussion-between-simplebob-and-buster). – Daniel Feb 04 '16 at 14:47

1 Answers1

0

Assuming that you will declare the viewInfo instance variable in ChemistryMasterViewController class you can do this.

var viewInfo:ChemistryViewInfo? //instance property declaration

    if let viewInfo = self.viewInfo  {
    ....
    ....
    }
Pradeep K
  • 3,671
  • 1
  • 11
  • 15
  • Would I do this within the viewDidLoad function? – Bigfoot11 Feb 04 '16 at 14:18
  • Yes, but you have to declare the viewInfo instance variable outside it along with the outlets that you have – Pradeep K Feb 04 '16 at 14:18
  • I am getting an error on all the lines inside the if-let statement: `Cannot pass immutable value of type 'UIButton' as inout argument`. How would I fix this? – Bigfoot11 Feb 04 '16 at 14:22
  • 1
    Remove inout for button argument and remove the & from the lines inside the if-let block – Pradeep K Feb 04 '16 at 14:36
  • That fixed the error but the buttons don't have the correct backgrounds or text, any idea? – Bigfoot11 Feb 04 '16 at 14:39
  • let buttonInfo = viewInfo.buttons[index] You should pass the index instead of hardcoding to 0. If this works please accept my answer – Pradeep K Feb 04 '16 at 16:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102622/discussion-between-buster-and-pradeep-k). – Bigfoot11 Feb 04 '16 at 17:08