0

そのオプションがどこにあるのかわかりません。助けてください!これは本当に必要です。コードでオプションの値を使用していません。

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
    }
}
4

1 に答える 1

-1

まず、すべてのアウトレットが潜在的に nil になる可能性があるため、最初に ? を追加することをお勧めします。オプションでそれらすべてをアンラップします。これを行うと、Optional が nil の場合にクラッシュする代わりに何もしません。以下で、これをいくつかのコードに追加しましたが、さらにいくつか追加する必要があるかもしれません。私が見落としていた他のオプションに。これを行う別の方法は、if-let アンラップを使用することです。これにより、"?" の量が減りますが、ネストの量が増えます。

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)"
    }
}
于 2015-07-04T17:46:46.090 に答える