在解包可选值时发现nil,错误

时间:2015-07-04 17:33:43

标签: ios swift null xcode6 optional-values

我不知道那个选项在哪里。请帮忙!我真的需要这个,我在代码中没有使用可选值。

import UIKit

class ViewController: UIViewController {

    //this is the outlet for the sliders RGB values
    @IBOutlet weak var redSlider: UISlider!
    @IBOutlet weak var greenSlider: UISlider!
    @IBOutlet weak var blueSlider: UISlider!

    //this are the outlets for the labels
    @IBOutlet weak var redLabelValue: UILabel!
    @IBOutlet weak var greenLabelValue: UILabel!
    @IBOutlet weak var blueLabelValue: UILabel!

    @IBOutlet weak var hexColor: UILabel!

    func changeThumbImage() {
        let redThumbImage = UIImage(named: "RedThumb@3x")
        redSlider.setThumbImage(redThumbImage, forState: .Normal)

        let greenThumbImage = UIImage(named: "GreenThumb@3x")
        greenSlider.setThumbImage(greenThumbImage, forState: .Normal)

        let blueThumbImage = UIImage(named: "BlueThumb@3x")
        blueSlider.setThumbImage(blueThumbImage, forState: .Normal)
    }

    func colorValues() {
        var roundedRed = UInt32(redSlider.value)
        var roundedGreen = UInt32(greenSlider.value)
        var roundedBlue = Int32(blueSlider.value)

        //converts the slider value to a string
        redLabelValue.text = String(stringInterpolationSegment: roundedRed)
        greenLabelValue.text = String(stringInterpolationSegment: roundedGreen)
        blueLabelValue.text = String(stringInterpolationSegment: roundedBlue)
    }

    func rgbValues() {
        var redValue = Int(redSlider.value)
        var greenValue = Int(greenSlider.value)
        var blueValue = Int(blueSlider.value)
    }

    //VIEW DID LOAD
    override func viewDidLoad() {
        super.viewDidLoad()
        updateBackgroundColor()
        colorValues()
        changeThumbImage()

        redSlider = UISlider(frame: CGRect(x: 100,y: 100,width: 200,height: 25))

    func getRandomColor() -> UIColor{

        let uniformRed = UInt32(redSlider.value)
        let uniformGreen = UInt32(greenSlider.value)
        let uniformBlue = UInt32(blueSlider.value)

        var randomRed:CGFloat = CGFloat(arc4random_uniform(uniformRed))
        var randomGreen:CGFloat = CGFloat(arc4random_uniform(uniformGreen))
        var randomBlue:CGFloat = CGFloat(arc4random_uniform(uniformBlue))

        return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
    }
    //view.backgroundColor = getRandomColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func updateBackgroundColor() {
        colorValues()
        //constant to set the value of the slider to a CGFloat
        let redValue = CGFloat(redSlider.value/255)
        let greenValue = CGFloat(greenSlider.value/255)
        let blueValue = CGFloat(blueSlider.value/255)
        //set the RGBA values
        view.backgroundColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1)

        //get the hexadecimal value from the RGB color values
        let hexValue = String(format:"%02X", Int(redSlider.value)) + String(format:"%02X", Int(greenSlider.value)) + String(format:"%02X", Int(blueSlider.value))

        hexColor.text = "#\(hexValue)"
    }

    //get rid of status bar
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分:-1)

嗯,首先,你的所有商店在某些时候可能都是零,所以一个良好的开端会增加?可选地打开它们全部。如果你这样做,它只会做什么,而不是在Optional为零的情况下崩溃。下面我将其添加到您的一些代码中,但您可能需要添加更多代码?我忽略了其他选项。另一种方法是使用if-let unwrapping,这会减少“?”的数量,但会增加嵌套量。

func changeThumbImage() {
    let redThumbImage = UIImage(named: "RedThumb@3x")
    redSlider?.setThumbImage(redThumbImage, forState: .Normal)

    let greenThumbImage = UIImage(named: "GreenThumb@3x")
    greenSlider?.setThumbImage(greenThumbImage, forState: .Normal)

    let blueThumbImage = UIImage(named: "BlueThumb@3x")
    blueSlider?.setThumbImage(blueThumbImage, forState: .Normal)
}

//// 
func colorValues() {
    var roundedRed = UInt32(redSlider.value)
    var roundedGreen = UInt32(greenSlider.value)
    var roundedBlue = Int32(blueSlider.value)

    //converts the slider value to a string
    redLabelValue?.text = String(stringInterpolationSegment: roundedRed)
    greenLabelValue?.text = String(stringInterpolationSegment: roundedGreen)
    blueLabelValue?.text = String(stringInterpolationSegment: roundedBlue)
}

也可以使用if-let来安全地打开滑块......

@IBAction func updateBackgroundColor() {
    colorValues()

    if let red = redSlider, green = greenSlider, blue = blueSlider{
        let redValue = CGFloat(red.value/255)
        let greenValue = CGFloat(green.value/255)
        let blueValue = CGFloat(blue.value/255)

        //set the RGBA values
        view.backgroundColor = UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 1)

        //get the hexadecimal value from the RGB color values
        let hexValue = String(format:"%02X", Int(redSlider.value)) + String(format:"%02X", Int(greenSlider.value)) + String(format:"%02X", Int(blueSlider.value))

        hexColor?.text = "#\(hexValue)"
    }
}