Swift,打印问题何时使用?而且!变量

时间:2016-07-18 13:12:50

标签: swift variables declaration

我在打印变量p02时遇到了访问错误('致命错误:在展开可选值时意外发现nil;#39;)。根据错误信息,我仍然无法理解原因。

var p02:Person! = nil
var p03:Person? = nil

if (p02==nil)
{
    print("p02 is nil")
}
if (p03==nil)
{
    print("p03 is nil")
}
print(p03)
print(p02)

任何人都知道为什么, 感谢

1 个答案:

答案 0 :(得分:1)

因为它总是试图打印p02和p03,即使它们是零

试试这个

var p02:Person! = nil
var p03:Person? = nil

if (p02==nil)
{
    print("p02 is nil")
}else{
    print(p02)
}
if (p03==nil)
{
    print("p03 is nil")
}else{
    print(p03)
}