[英]Swift Optionals - Unexpectedly found nil when unwrapping an optional value
I'm new to Swift and have been trying to wrap (ha) my head around optional values. 我是Swift的新手,并且一直在尝试将可选值包装起来。 As far as I can see - although I'm probably wrong - variables can be optional and therefore contain a nil value and these have to be accounted for in code.
据我所知-尽管我可能是错的-变量可以是可选的,因此包含nil值,并且这些必须在代码中说明。
Whenever I hit the 'save' button in my application I get the error: 'fatal error: unexpectedly found nil while unwrapping an Optional value'. 每当我在应用程序中单击“保存”按钮时,都会收到错误消息:“致命错误:在展开可选值时意外发现nil”。
@IBAction func saveTapped(sender: AnyObject) {
//code to save fillup to Parse backend
// if variable is not nil
if let username = PFUser.currentUser()?.username {
// set username equal to current user
self.object.username = username
}else{
println("PFUser.currentUser()?.username contained a nil value.")
}
// if variable is not nil
if let amount = self.amountTextField.text {
//set amount equal to value in amountTextField
self.object.amount = self.amountTextField.text
}else{
println("self.amountTextField.text contained a nil value.")
}
// if variable is not nil
if let cost = self.costTextField.text {
// set cost equal to the value in costTextField
self.object.cost = self.costTextField.text
}else{
println("self.costTextField.text contained a nil value.")
}
// set date equal to the current date
self.object.date = self.date
//save the object
self.object.saveEventually { (success, error) -> Void in
if error == nil {
println("Object saved!")
}else{
println(error?.userInfo)
}
}
// unwind back to root view controller
self.navigationController?.popToRootViewControllerAnimated(true)
}
Not sure if the error is because of something in this block of code or somewhere else - can provide the main class code if needed. 不知道错误是由于此代码块中的某处还是其他地方引起的-如果需要,可以提供主类代码。
Any help anyone can provided would be really appreciated as this has been bugging me for a while now. 任何人都可以提供的任何帮助将不胜感激,因为这已经困扰了我一段时间。
From your code and the comments it sounds like your issue definitely lies with self.object
从您的代码和注释看来,您的问题肯定在
self.object
Your code never uses an if let
statement to check to ensure self.object is not nil 您的代码从不使用
if let
语句检查以确保self.object不为nil
Using println(username)
works because your username
is not nil. 使用
println(username)
可以工作,因为您的username
不是nil。 But when you try to call self.object.username
, it's the self.object
that is causing the crash. 但是,当您尝试调用
self.object.username
,是导致崩溃的self.object
。
You may have a property in your implementation like var object:CustomPFObject!
您的实现中可能具有
var object:CustomPFObject!
类的属性var object:CustomPFObject!
which means, the first time you access this variable it's expected to not be nil. 这意味着,第一次访问此变量时,它不会为nil。 You'll probably want to check the code where you are setting self.object for the first time, and to make sure that it's being set before you've tried to access it.
您可能需要检查一下第一次设置self.object的代码,并确保在尝试访问它之前已对其进行了设置。
If you're not able to manage when self.object
is set, and when it's accessed, then change your declaration to var object:CustomPFObject?
如果您无法管理何时设置
self.object
以及何时对其进行访问,则将声明更改为var object:CustomPFObject?
Now it's an optional, and as you write your code you'll be forced to make decisions as you go along. 现在,它是可选的,并且在编写代码时,您将被迫在执行过程中做出决定。
For example: 例如:
var object:CustomPFObject?
let text = self.object.username //won't compile
let text = self.object!.username //mighty crash
let text = self.object?.username //you're safe, 'text' just ends up nil
I hope this helps you solve your issue. 希望这可以帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.