展开可选值 (UIButton) 时意外发现 nil
Unexpectedly found nil while unwrapping an Optional value (UIButton)
我正在尝试以编程方式创建一个按钮并将其添加到 Swift 中的 UIScrollView。下面的代码是我正在使用的,并在我将按钮添加为子视图的地方抛出了臭名昭著的 "unwrapping optional error" 。为什么要这样做?我已经检查并将我的代码与其他人的代码进行比较,一切看起来都很好。
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button) //This line throws error
提前致谢。
尝试修复第一行(as!)
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
The as! Operator - developer.apple.com/swift/blog
Swift 1.2 separates the notions of guaranteed conversion and forced conversion into two distinct operators. Guaranteed conversion is still performed with theas
operator, but forced conversion now uses the as!
... It may be easiest to remember the pattern for these operators in Swift as: !
implies “this might trap,” while ?
indicates “this might be nil.”
Swift 2 版本(Xcode7 测试版 2):
let button = UIButton(type: .System) as UIButton
// see that the way we do this has changed
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button)
Swift 1.2 版本 (Xcode 6.3.2):
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button)
我正在尝试以编程方式创建一个按钮并将其添加到 Swift 中的 UIScrollView。下面的代码是我正在使用的,并在我将按钮添加为子视图的地方抛出了臭名昭著的 "unwrapping optional error" 。为什么要这样做?我已经检查并将我的代码与其他人的代码进行比较,一切看起来都很好。
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button) //This line throws error
提前致谢。
尝试修复第一行(as!)
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
The as! Operator - developer.apple.com/swift/blog
Swift 1.2 separates the notions of guaranteed conversion and forced conversion into two distinct operators. Guaranteed conversion is still performed with the
as
operator, but forced conversion now uses theas!
... It may be easiest to remember the pattern for these operators in Swift as:!
implies “this might trap,” while?
indicates “this might be nil.”
Swift 2 版本(Xcode7 测试版 2):
let button = UIButton(type: .System) as UIButton
// see that the way we do this has changed
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button)
Swift 1.2 版本 (Xcode 6.3.2):
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button)