展开可选值时发现 nil,错误
Found nil while unwrapping an Optional value, Error
我不知道那个可选的在哪里。请帮忙!我真的需要这个,我没有在我的代码中使用可选值。
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
}
}
好吧,首先,您的所有网点在某个时候都可能为零,所以一个好的开始是添加 ?可选择全部解包。如果你这样做,它只会在 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)"
}
}
我不知道那个可选的在哪里。请帮忙!我真的需要这个,我没有在我的代码中使用可选值。
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
}
}
好吧,首先,您的所有网点在某个时候都可能为零,所以一个好的开始是添加 ?可选择全部解包。如果你这样做,它只会在 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)"
}
}