[string] swift How to remove optional String Character

How do I remove Optional Character


let color = colorChoiceSegmentedControl.titleForSegmentAtIndex(colorChoiceSegmentedControl.selectedSegmentIndex)

println(color) // Optional("Red")

let imageURLString = "http://hahaha.com/ha.php?color=\(color)"
println(imageURLString)
//http://hahaha.com/ha.php?color=Optional("Red")

I just want output "http://hahaha.com/ha.php?color=Red"

How can I do?

hmm....

This question is related to string swift

The answer is


Try this,

var check:String?="optional String"
print(check!) //optional string. This will result in nil while unwrapping an optional value if value is not initialized or if initialized to nil.
print(check) //Optional("optional string") //nil values are handled in this statement

Go with first if you are confident to have no nil in your variable. Also, you can use if let or Guard let statement to unwrap optionals without any crash.

 if let unwrapperStr = check
 {
    print(unwrapperStr) //optional String
 }

Guard let,

 guard let gUnwrap = check else
 {
    //handle failed unwrap case here
 }
 print(gUnwrap) //optional String

I looked over this again and i'm simplifying my answer. I think most the answers here are missing the point. You usually want to print whether or not your variable has a value and you also want your program not to crash if it doesn't (so don't use !). Here just do this

    print("color: \(color ?? "")")

This will give you blank or the value.


You need to unwrap the optional before you try to use it via string interpolation. The safest way to do that is via optional binding:

if let color = colorChoiceSegmentedControl.titleForSegmentAtIndex(colorChoiceSegmentedControl.selectedSegmentIndex) {
    println(color) // "Red"

    let imageURLString = "http://hahaha.com/ha.php?color=\(color)"
    println(imageURLString) // http://hahaha.com/ha.php?color=Red
}

Although we might have different contexts, the below worked for me.

I wrapped every part of my variable in brackets and then added an exclamation mark outside the right closing bracket.

For example, print(documentData["mileage"]) is changed to:

print((documentData["mileage"])!)

Actually when you define any variable as a optional then you need to unwrap that optional value. To fix this problem either you have to declare variable as non option or put !(exclamation) mark behind the variable to unwrap the option value.

var temp : String? // This is an optional.
temp = "I am a programer"                
print(temp) // Optional("I am a programer")

var temp1 : String! // This is not optional.
temp1 = "I am a programer"
print(temp1) // "I am a programer"

You can just put ! and it will work:

print(var1) // optional("something")

print(var1!) // "something"

Simple convert ? to ! fixed my issue:

usernameLabel.text = "\(userInfo?.userName)"

To

usernameLabel.text = "\(userInfo!.userName)"

when you define any variable as a optional then you need to unwrap that optional value.Convert ? to !


In swift3 you can easily remove optional

if let value = optionalvariable{
 //in value you will get non optional value
}

Hello i have got the same issue i was getting Optional(3) So, i have tried this below code

cell.lbl_Quantity.text = "(data?.quantity!)" //"Optional(3)"

let quantity = data?.quantity

cell.lbl_Quantity.text = "(quantity!)" //"3"


import UIKit

let characters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

var a: String = characters.randomElement()!
var b: String = characters.randomElement()!
var c: String = characters.randomElement()!
var d: String = characters.randomElement()!
var e: String = characters.randomElement()!
var f: String = characters.randomElement()!
var password = ("\(a)" + "\(b)" + "\(c)" + "\(d)" + "\(e)" + "\(f)")

    print ( password)

Besides the solutions mentioned in other answers, if you want to always avoid that Optional text for your whole project, you can add this pod:

pod 'NoOptionalInterpolation'

(https://github.com/T-Pham/NoOptionalInterpolation)

The pod adds an extension to override the string interpolation init method to get rid of the Optional text once for all. It also provides a custom operator * to bring the default behaviour back.

So:

import NoOptionalInterpolation
let a: String? = "string"
"\(a)"  // string
"\(a*)" // Optional("string")

Please see this answer https://stackoverflow.com/a/37481627/6390582 for more details.


print("imageURLString = " + imageURLString!)

just use !


Check for nil and unwrap using "!":

let color = colorChoiceSegmentedControl.titleForSegmentAtIndex(colorChoiceSegmentedControl.selectedSegmentIndex)

println(color) // Optional("Red")

if color != nil {
    println(color!) // "Red"
    let imageURLString = "http://hahaha.com/ha.php?color=\(color!)"
    println(imageURLString)
    //"http://hahaha.com/ha.php?color=Red"
}