Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift主细节模板应用程序之谜_Ios_Swift_Iboutlet_Optional - Fatal编程技术网

Ios Swift主细节模板应用程序之谜

Ios Swift主细节模板应用程序之谜,ios,swift,iboutlet,optional,Ios,Swift,Iboutlet,Optional,我收到一个无法展开的可选错误。在Swift中运行稍微修改过的Master Detail应用程序时,没有一个错误 我所做的只是将第二个UILabel添加到DetailViewController中,就在预先存在的detailDescriptionLabel下,当我从MasterViewController导航到DetailViewController时,我会崩溃设置新标签的语句: secondLabel.text = "This is the Second Label" 我声明此

我收到一个
无法展开的可选错误。在Swift中运行稍微修改过的Master Detail应用程序时,没有一个
错误

我所做的只是将第二个
UILabel
添加到DetailViewController中,就在预先存在的
detailDescriptionLabel
下,当我从MasterViewController导航到DetailViewController时,我会崩溃设置新标签的语句:

        secondLabel.text = "This is the Second Label"
我声明此标签如下:

@IBOutlet var secondLabel : UILabel
真正有趣的是,用于设置
detailDescriptionLabel
的现有代码包括新的可选
let
语法:

        if let label = self.detailDescriptionLabel {
            label.text = "This is the Detail View"
        }
那么为什么我们需要一个
let
语句来表示
detailDescriptionLabel
?它从未声明为可选标签,它与任何常规标签IBOutlet属性一样声明,如下所示:

@IBOutlet var detailDescriptionLabel: UILabel
那么,为什么它被视为可选的呢

这是否意味着从现在起,我添加为IBOutlet的任何对象如果要通过代码进行设置,也必须通过这种
let
语句

编辑:

我在以下方法中崩溃,在
anotherLabel.text=“Second Label”
行:

但是,当我用整个
if let
业务来处理另一个标签时,如下所示,它工作得非常好:

if let label2 = self.anotherLabel {
    label2.text = "Second Label"
} 

@IBOutlet
声明的属性始终是隐式展开的可选变量。解释如下:

在Swift中声明出口时,编译器会自动 将类型转换为弱隐式展开的可选类型并指定 它的初始值为零。实际上,编译器替换了
@IBOutlet-var-name:Type
带有
@IBOutlet-var-name:Type!=无
。 编译程序 将类型转换为隐式展开的可选类型,以便 不需要在初始值设定项中赋值。这是含蓄的 取消包装,因为在从情节提要或 在xib文件中,可以假定插座已连接。经销店 默认情况下是弱的,因为您创建的插座通常具有弱的 关系

因为它是隐式展开的,所以如果每次都让label=something
,您就不必去检查
,只要知道如果您的标签是
nil
,并且您尝试使用它,您将以运行时错误告终。我猜你的第二个标签没有连接到Interface Builder中——是这样吗?[作品:不!]


好的,在这种特定情况下发生的是,可以从主视图控制器的
prepareforsgue()
调用
configureView()
方法,因为详细视图控制器上的
detailItem
属性有一个
didSet
处理程序。发生这种情况时,局部视图控制器尚未加载,因此未创建任何标签。由于标签将同时设置,因此您可以将两个初始化都放在一个if语句下(甚至使其更清晰):


我的第二个标签与Interface Builder完全相连!:-)像往常一样,我把它拖放到代码中,所以它肯定是连接在一起的。(我还与Connections Inspector进行了双重检查。)那么,如果我想为标签分配文本,我如何不通过
呢?你在用什么方法?(如果方便的话,你可以在gist.github.com上把整件事都说出来。)当然可以-我在我的原始问题中添加了代码-见上文。哈哈!接得好!:-)这很有道理。非常感谢-标记为正确答案:-)
if let label2 = self.anotherLabel {
    label2.text = "Second Label"
} 
func configureView() {
    // Update the user interface for the detail item.
    if let theCar: CarObject = self.detailItem as? CarObject {
        if self.detailDescriptionLabel != nil {
            self.detailDescriptionLabel.text = theCar.make
            self.secondLabel.text = "Second Label"
        }
    }
}