Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrés Leal
9,563 PointsPlease help! "Found nil while unwrapping an Optional value"
This is all my code! haha sorry but I don't know where is that optional value or something haha, please help! I really need this!
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
}
}