展开可选值时意外发现 nil:swift 2.1
unexpectedly found nil while unwrapping an Optional value : swift 2.1
我的应用程序在转换为新的 Swift 之前一直运行良好..
当我在包含表
的某些视图中测试应用程序时出现此错误
fatal error: unexpectedly found nil while unwrapping an Optional value
这里是app每次碾压的函数:
public func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if(self.hasValue == true && self.beginNewObject == true){
print("New employee..")
var stf : Staff = Staff();
self.staffs?.append(stf);
self.beginNewObject = false;
}
if (self.currentElementValue == nil)
{self.currentElementValue = "";}
switch elementName{
case "DepartmentName":
self.staffs?.last!.DepartmentName = self.currentElementValue;
case "DepartmentNumber":
self.staffs?.last!.DepartmentNumber = self.currentElementValue;
case "Center":
self.staffs?.last!.Center = self.currentElementValue;
case "Display_StaffResult":
print("DisplayResult");
default:
print("default case");
}
self.hasValue = false;
}
而且我不知道在哪里更改代码,因为没有错误!!
错误发生在你强制解包最后 属性 员工对象的一行中:
self.staffs?.last!
通过使用!你说我确定我的可选数据中有数据,但不幸的是你错了,这就是应用崩溃的原因。
您可以将其更改为:
self.staffs?.last?...
这是一种安全的方式或这样做:
if let last = self.staffs?.last {
last.DepartmentName = ...
}
您似乎打开了可能不存在的 .last!
元素(空数组或 self.staffs 不存在)。
尝试将对 self.staffs?.last!
的每次调用替换为以下内容:
if let lastElem = staffs?.last {
lastElem = ...
}
或者在你的 switch 之前添加这个并将 switch 放在这个括号内。
替换“!”用“?”
因为您用“!”展开了一些 nil 值
尝试添加异常断点
我的应用程序在转换为新的 Swift 之前一直运行良好.. 当我在包含表
的某些视图中测试应用程序时出现此错误fatal error: unexpectedly found nil while unwrapping an Optional value
这里是app每次碾压的函数:
public func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if(self.hasValue == true && self.beginNewObject == true){
print("New employee..")
var stf : Staff = Staff();
self.staffs?.append(stf);
self.beginNewObject = false;
}
if (self.currentElementValue == nil)
{self.currentElementValue = "";}
switch elementName{
case "DepartmentName":
self.staffs?.last!.DepartmentName = self.currentElementValue;
case "DepartmentNumber":
self.staffs?.last!.DepartmentNumber = self.currentElementValue;
case "Center":
self.staffs?.last!.Center = self.currentElementValue;
case "Display_StaffResult":
print("DisplayResult");
default:
print("default case");
}
self.hasValue = false;
}
而且我不知道在哪里更改代码,因为没有错误!!
错误发生在你强制解包最后 属性 员工对象的一行中:
self.staffs?.last!
通过使用!你说我确定我的可选数据中有数据,但不幸的是你错了,这就是应用崩溃的原因。
您可以将其更改为:
self.staffs?.last?...
这是一种安全的方式或这样做:
if let last = self.staffs?.last {
last.DepartmentName = ...
}
您似乎打开了可能不存在的 .last!
元素(空数组或 self.staffs 不存在)。
尝试将对 self.staffs?.last!
的每次调用替换为以下内容:
if let lastElem = staffs?.last {
lastElem = ...
}
或者在你的 switch 之前添加这个并将 switch 放在这个括号内。
替换“!”用“?”
因为您用“!”展开了一些 nil 值
尝试添加异常断点