The reason here:
self.xaxis.text is an optional (may be nil).
So you have to address it with the nil coalescing operator
var xoutput = self.xaxis.text ?? "0"
Which is equivalent to:
var xoutput : String
if self.xaxis.text != nil {
xoutput = self.xaxis.text!
} else {
xoutput = "0"
}
or a bit more compact:
var xoutput = "0"
if self.xaxis.text != nil {
xoutput = self.xaxis.text!
}
For more on this: https://www.hackingwithswift.com/example-code/language/what-is-the-nil-coalescing-operator
Note that in the example
print("Hello, \(name ?? "Anonymous")!")
the final ! is not for unwrapping, it is just an exclamation point to be printed at the end of text.
If you were sure it cannot be nil, you could also unwrap directly, but that is really risky.
var xoutput = self.xaxis.text!
Once you are sure that string is a string, not an optional, you can pass it to Double() to convert String to Double.
But here again, if the content is not a number (eg: "abc", Double will return nil. And in all cases it returns an optional.
So here again, use the nil coalescing operator to unwrap (transform optional to a real value) safely.