Home>
An error occurs when you unwrap. What should I do?
@ IBOutlet weak var str: UILabel!
func doubleNum ()
{
// Value of optional type 'String?' Must be unwrapped to a value of type 'String'
var db: Double = NSString (string: str.text) .doubleValue
}
-
Answer # 1
-
Answer # 2
Unwrap using
if let
,guard let
,??
or whatever is appropriate.Example:
if let db = NSString (string: str.text) .doubleValue { // If the string can be converted to Double type } else { // If conversion failed }
Related articles
- swift - i want to convert an int to a string with a specified number of digits padded with spaces
- swift - i want to reflect the picker value in label
- cannot get uid value of currentuser in swift (authentication by name only)
- i want to convert a string into a mathematical expression in java
- swift - i want to pass a value to view of containerview of transition destination
- swift - unterminated string literal
- swift - i want to put a default value in realm
- in cgi script of python, i want to get the query string when the query string of the url is "? value"
- convert integer number to string year/month display in java
- how to convert a string to date type in python
- convert double to string in java
- i want to convert from string to int type in golang and convert uppercase to lowercase using ascii table
- c# - i want to convert a string type to a class and attach it to an object
- [c] i want to convert a string to a number, but it doesn't work
- unity - cannot convert float type to string type
- aspnet - how to connect string and value in navigateurl="" in vbnet
- python - could not convert string to float error
- ruby - i want to get the cookie value as a string
- string - i want to display the pointer value
- i want to pass the character string entered in swift text field to the screen transition destination by value
Trends
By the way, to make String a Double
is enough.