【问题标题】:Creating a simple messaging App on iOS and keep getting the same error在 iOS 上创建一个简单的消息应用程序并不断收到相同的错误
【发布时间】:2018-08-05 15:46:26
【问题描述】:
我正在 iOS 上创建一个简单的消息传递应用程序并不断收到相同的错误
不断弹出错误的代码行就是这一行
self.listOfChatInfo.append(Chat(userName: username!, text: text!, datePosted: "\(postDate!)"))
不断弹出的错误是
线程 1:致命错误:在展开可选值时意外发现 nil
我才刚刚开始,这是来自一个在线小视频教程,所以对他们没有任何帮助。
感谢您的宝贵时间。
【问题讨论】:
标签:
swift
debugging
error-handling
【解决方案1】:
我觉得没问题。
let element = Chat(userName: username!, text: text!, datePosted: "\(postDate!))
self.listOfChatInfo.append(element)
【解决方案2】:
确保username、text 和postDate 不为零。
您可以使用此代码来避免运行时错误。
let userName = username ?? ""
let chatText = text ?? ""
let date = postDate ?? ""
self.listOfChatInfo.append(Chat(userName: userName, text: chatText, datePosted: date))