向后传递数据时解包 Optional 值时意外发现 nil
Unexpectedly found nil while unwrapping an Optional value when passing data backward
我已经学习了 Swift 和 Xcode 大约 3 周,我正在做一个待办事项应用程序只是为了练习。但是,当我尝试将新项目添加到我的待办事项列表时,出现了 "Unexpectedly found nil while unwrapping an Optional value".
错误
我有 Main.storyboard 创建的主屏幕和主屏幕上的添加按钮。当单击该按钮时,它会转到我通过 Eureka 框架创建的新 ViewController,用户可以在该表单中输入一些信息,如标题、描述、类别,并将它们传递回主屏幕。我使用委托协议传递数据,当我单击“保存项目”按钮时,应用程序崩溃了,它说
"Fatal error: Unexpectedly found nil while unwrapping an Optional value".
代码如下:
import Foundation
import Eureka
protocol CanReceive {
func dataReceived(data: ToDo)
}
class AddItemViewController : FormViewController {
var delegate : CanReceive?
var todoItem : ToDo?
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm a"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
form +++ Section()
<<< TextRow(){
[=10=].title = "Title"
[=10=].placeholder = "Enter text here"
[=10=].onChange { [unowned self] row in
self.todoItem?.title = row.value
}
}
<<< TextRow(){
[=10=].title = "Description"
[=10=].placeholder = "Give some description"
[=10=].onChange { [unowned self] row in
self.todoItem?.description = row.value
}
}
<<< AlertRow<String>() {
[=10=].title = "Category"
[=10=].selectorTitle = "Select the category"
[=10=].options = ["Personal ", "Home ", "Work ", "Play ", "Health ♀️" , "Other"]
[=10=].onChange { [unowned self] row in
self.todoItem?.category = row.value
}
}
+++ Section(){ section in
section.header = {
var header = HeaderFooterView<UIView>(.callback({
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
button.backgroundColor = .darkGray
button.setTitle("Save Item", for: .normal)
button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
return button
}))
header.height = { 50 }
return header
}()
}
}
@objc func buttonAction(sender: UIButton!) {
delegate?.dataReceived(data: todoItem!)
self.dismiss(animated: true, completion: nil)
print("Button tapped")
}
}
Todo 类型定义为:
struct ToDo {
var title: String?
var description: String?
var category : String?
}
这里是控制 main.storyboard 的 ViewController :
import UIKit
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CanReceive {
var todoList = [ToDo]()
let cellId = "CellId"
let test = ["Row1", "Row2", "Row3"]
let imageName = ["anchor", "arrow-down", "aperture"]
@IBOutlet weak var ImageTop: UIImageView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: cellId)
configureTableView()
ImageTop.image = UIImage(named: "todo-background")
}
//data source method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableViewCell
cell.categoryLabel?.text = todoList[indexPath.row].category
cell.descriptionLabel.text = todoList[indexPath.row].description
var imageCategory = ""
switch todoList[indexPath.row].category {
case "Personal ":
imageCategory = "Personal"
case "Home ":
imageCategory = "Home"
case "Work ":
imageCategory = "Work"
case "Play ":
imageCategory = "Play"
case "Health ♀️":
imageCategory = "Health"
case "Other":
imageCategory = "Other"
default:
break
}
cell.imageCategory?.image = UIImage(named: imageCategory)
return cell
}
func configureTableView() {
self.tableView.rowHeight = 80
self.tableView.separatorStyle = .none
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Did select row at index \(indexPath.row)")
print("Row height is: \(tableView.rowHeight)")
}
@IBAction func addButtonPressed(_ sender: RoundButton) {
let addItemViewController = AddItemViewController()
addItemViewController.delegate = self
self.present(addItemViewController, animated:true, completion:nil)
}
//Delegate
func dataReceived(data: ToDo) {
todoList.append(data)
return
}
}
感谢您抽出宝贵时间,如果我的问题太愚蠢,我深表歉意。
错误发生是因为在 AddItemViewController
中声明了 属性 todoItem
但未初始化。
我建议为这三个属性使用临时变量,并在按下按钮时创建一个 ToDo
实例
class AddItemViewController : FormViewController {
var delegate : CanReceive?
var title, description, category : String?
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm a"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
form +++ Section()
<<< TextRow(){
[=10=].title = "Title"
[=10=].placeholder = "Enter text here"
[=10=].onChange { [unowned self] row in
self.title = row.value
}
}
<<< TextRow(){
[=10=].title = "Description"
[=10=].placeholder = "Give some description"
[=10=].onChange { [unowned self] row in
self.description = row.value
}
}
<<< AlertRow<String>() {
[=10=].title = "Category"
[=10=].selectorTitle = "Select the category"
[=10=].options = ["Personal ", "Home ", "Work ", "Play ", "Health ♀️" , "Other"]
[=10=].onChange { [unowned self] row in
self.category = row.value
}
}
+++ Section(){ section in
section.header = {
var header = HeaderFooterView<UIView>(.callback({
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
button.backgroundColor = .darkGray
button.setTitle("Save Item", for: .normal)
button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
return button
}))
header.height = { 50 }
return header
}()
}
}
@objc func buttonAction(sender: UIButton) { // no implicit unwrapped optional
let todoItem = ToDo(title: title, description: description, category : category)
delegate?.dataReceived(data: todoItem)
self.dismiss(animated: true, completion: nil)
print("Button tapped")
}
}
我认为这一行 delegate?.dataReceived(data: todoItem!)
导致了您的错误,因为您在 todoItem 未初始化时隐式解包了它
我已经学习了 Swift 和 Xcode 大约 3 周,我正在做一个待办事项应用程序只是为了练习。但是,当我尝试将新项目添加到我的待办事项列表时,出现了 "Unexpectedly found nil while unwrapping an Optional value".
错误我有 Main.storyboard 创建的主屏幕和主屏幕上的添加按钮。当单击该按钮时,它会转到我通过 Eureka 框架创建的新 ViewController,用户可以在该表单中输入一些信息,如标题、描述、类别,并将它们传递回主屏幕。我使用委托协议传递数据,当我单击“保存项目”按钮时,应用程序崩溃了,它说
"Fatal error: Unexpectedly found nil while unwrapping an Optional value".
代码如下:
import Foundation
import Eureka
protocol CanReceive {
func dataReceived(data: ToDo)
}
class AddItemViewController : FormViewController {
var delegate : CanReceive?
var todoItem : ToDo?
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm a"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
form +++ Section()
<<< TextRow(){
[=10=].title = "Title"
[=10=].placeholder = "Enter text here"
[=10=].onChange { [unowned self] row in
self.todoItem?.title = row.value
}
}
<<< TextRow(){
[=10=].title = "Description"
[=10=].placeholder = "Give some description"
[=10=].onChange { [unowned self] row in
self.todoItem?.description = row.value
}
}
<<< AlertRow<String>() {
[=10=].title = "Category"
[=10=].selectorTitle = "Select the category"
[=10=].options = ["Personal ", "Home ", "Work ", "Play ", "Health ♀️" , "Other"]
[=10=].onChange { [unowned self] row in
self.todoItem?.category = row.value
}
}
+++ Section(){ section in
section.header = {
var header = HeaderFooterView<UIView>(.callback({
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
button.backgroundColor = .darkGray
button.setTitle("Save Item", for: .normal)
button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
return button
}))
header.height = { 50 }
return header
}()
}
}
@objc func buttonAction(sender: UIButton!) {
delegate?.dataReceived(data: todoItem!)
self.dismiss(animated: true, completion: nil)
print("Button tapped")
}
}
Todo 类型定义为:
struct ToDo {
var title: String?
var description: String?
var category : String?
}
这里是控制 main.storyboard 的 ViewController :
import UIKit
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CanReceive {
var todoList = [ToDo]()
let cellId = "CellId"
let test = ["Row1", "Row2", "Row3"]
let imageName = ["anchor", "arrow-down", "aperture"]
@IBOutlet weak var ImageTop: UIImageView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: cellId)
configureTableView()
ImageTop.image = UIImage(named: "todo-background")
}
//data source method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todoList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableViewCell
cell.categoryLabel?.text = todoList[indexPath.row].category
cell.descriptionLabel.text = todoList[indexPath.row].description
var imageCategory = ""
switch todoList[indexPath.row].category {
case "Personal ":
imageCategory = "Personal"
case "Home ":
imageCategory = "Home"
case "Work ":
imageCategory = "Work"
case "Play ":
imageCategory = "Play"
case "Health ♀️":
imageCategory = "Health"
case "Other":
imageCategory = "Other"
default:
break
}
cell.imageCategory?.image = UIImage(named: imageCategory)
return cell
}
func configureTableView() {
self.tableView.rowHeight = 80
self.tableView.separatorStyle = .none
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Did select row at index \(indexPath.row)")
print("Row height is: \(tableView.rowHeight)")
}
@IBAction func addButtonPressed(_ sender: RoundButton) {
let addItemViewController = AddItemViewController()
addItemViewController.delegate = self
self.present(addItemViewController, animated:true, completion:nil)
}
//Delegate
func dataReceived(data: ToDo) {
todoList.append(data)
return
}
}
感谢您抽出宝贵时间,如果我的问题太愚蠢,我深表歉意。
错误发生是因为在 AddItemViewController
中声明了 属性 todoItem
但未初始化。
我建议为这三个属性使用临时变量,并在按下按钮时创建一个 ToDo
实例
class AddItemViewController : FormViewController {
var delegate : CanReceive?
var title, description, category : String?
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm a"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
form +++ Section()
<<< TextRow(){
[=10=].title = "Title"
[=10=].placeholder = "Enter text here"
[=10=].onChange { [unowned self] row in
self.title = row.value
}
}
<<< TextRow(){
[=10=].title = "Description"
[=10=].placeholder = "Give some description"
[=10=].onChange { [unowned self] row in
self.description = row.value
}
}
<<< AlertRow<String>() {
[=10=].title = "Category"
[=10=].selectorTitle = "Select the category"
[=10=].options = ["Personal ", "Home ", "Work ", "Play ", "Health ♀️" , "Other"]
[=10=].onChange { [unowned self] row in
self.category = row.value
}
}
+++ Section(){ section in
section.header = {
var header = HeaderFooterView<UIView>(.callback({
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
button.backgroundColor = .darkGray
button.setTitle("Save Item", for: .normal)
button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
return button
}))
header.height = { 50 }
return header
}()
}
}
@objc func buttonAction(sender: UIButton) { // no implicit unwrapped optional
let todoItem = ToDo(title: title, description: description, category : category)
delegate?.dataReceived(data: todoItem)
self.dismiss(animated: true, completion: nil)
print("Button tapped")
}
}
我认为这一行 delegate?.dataReceived(data: todoItem!)
导致了您的错误,因为您在 todoItem 未初始化时隐式解包了它