Ios swift阵列赢得';不包含实例变量
每次运行此命令时,我都会收到一个运行时错误:“致命错误:在展开可选值时意外发现nil”,引用位Ios swift阵列赢得';不包含实例变量,ios,arrays,swift,Ios,Arrays,Swift,每次运行此命令时,我都会收到一个运行时错误:“致命错误:在展开可选值时意外发现nil”,引用位var barItems:[UIBarButtonItem]=[self.nextKeyboardButton、self.captureButton、self.libraryButton],调试器声明barItems是空数组(nil) 这是怎么回事?为什么数组中的BarItem不包含我的实例UIBarButtonim变量?UIBarButtonim类型的变量(captureButton、libraryB
var barItems:[UIBarButtonItem]=[self.nextKeyboardButton、self.captureButton、self.libraryButton]
,调试器声明barItems
是空数组(nil)
这是怎么回事?为什么数组中的BarItem不包含我的实例UIBarButtonim变量?UIBarButtonim类型的变量(captureButton、libraryButton和nextKeyboardButton)本身似乎为零,但为什么呢?提前谢谢 许多来自objective C的对象都是隐式展开的选项,因为objective C指针没有swift提供的相同类型的安全性 这一行出现错误是因为类型
[UIBarButtonItem]
不允许在数组中使用nil值,并且您在数组中输入的一些值是nil
import UIKit
class KeyboardViewController: UIInputViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var nextKeyboardButton: UIBarButtonItem
var statusLabel: UILabel = UILabel()
var bottomBar : UIToolbar = UIToolbar()
var globeIcon : UIImage = UIImage(named: "globe-hover.png")
@IBOutlet var captureButton : UIBarButtonItem
@IBOutlet var libraryButton : UIBarButtonItem
override func viewDidLoad() {
super.viewDidLoad()
self.statusLabel.text="viewDidLoad() called"
// Perform custom UI setup here
self.nextKeyboardButton = UIBarButtonItem(image: self.globeIcon, style: .Plain , target: self, action: "advanceToNextInputMode")
self.captureButton = UIBarButtonItem(title: "Take Photo", style: .Plain, target: self, action: "captureClicked")
self.libraryButton = UIBarButtonItem(title: "Library", style: .Plain, target: self, action: "libraryClicked")
self.statusLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
self.statusLabel.sizeToFit()
var barItems : [UIBarButtonItem] = [self.nextKeyboardButton, self.captureButton, self.libraryButton]
self.bottomBar.items = barItems
self.view.addSubview(self.bottomBar)
self.view.addSubview(self.statusLabel)
var statusLabelLeftSideConstraint = NSLayoutConstraint(item: self.statusLabel, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1.0, constant: 0.0)
var statusLabelBottomConstraint = NSLayoutConstraint(item: self.statusLabel, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([statusLabelLeftSideConstraint, statusLabelBottomConstraint])
}
要使调试更简单,我要做的第一件事是将类型添加到这些值的原始实例化中(您不希望这些值是nil
):
objective-c中的一些初始值设定项返回nil以指示初始化失败,这里可能就是这种情况?如果是,则在添加类型注释后,将出现不同的错误。您可以发布该错误吗?若要使您的barItems数组接受nil值,请将类型更改为
[uiBarButtonim?]
。然后,您需要显式检查每个数组项,以查看它是否为nil,然后再使用if let-concreteItem=barItems[n]{.code when not nil.}else{.code when nil.}
construct。提示:在视图中创建数组将出现。在执行一些测试后,项目的初始值设定项肯定会返回nil
导致所有问题。在viewDidLoad()方法之前实例化了它们之后,我得到了错误信息,即对于第二个和第三个UIBarButtonims,“viewController.Type”没有名为“globeIcon”的成员,,调用中缺少参数“LandscapeMagePhone”的参数。在检查文档之后,我不应该提供那个论点,所以我不明白那里出了什么问题。我也不理解globeIcon的问题。有什么见解吗?当我错误地将[UIBarButtonItem]分配给navigationItem.rightBarButtonItem(而不是rightBarButtonItems)时,我得到了“调用中参数'Landscape ImagePhone'缺少参数”的信息,duh!
var barItems : [UIBarButtonItem] = [self.nextKeyboardButton, self.captureButton, self.libraryButton]
self.nextKeyboardButton : UIBarButtonItem = UIBarButtonItem(image: self.globeIcon, style: .Plain , target: self, action: "advanceToNextInputMode")
self.captureButton : UIBarButtonItem = UIBarButtonItem(title: "Take Photo", style: .Plain, target: self, action: "captureClicked")
self.libraryButton : UIBarButtonItem = UIBarButtonItem(title: "Library", style: .Plain, target: self, action: "libraryClicked")