如何快速删除可选字符串字符

如何删除可选字符


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")

我只想输出“ http://hahaha.com/ha.php?color=Red

我该怎么办?

嗯..。

121633 次浏览

在尝试通过字符串插值使用它之前,您需要展开可选项。最安全的方法是通过 可选绑定:

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
}

实际上,当您将任何变量定义为可选值时,您需要打开该可选值。要解决这个问题,您必须声明变量为非选项或放置!(感叹号)在变量后面标记以展开选项值。

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"

检查是否为空,并使用“ !”:

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"
}

我又看了一遍,我在简化我的答案。我认为这里的大多数答案都没有抓住重点。您通常希望输出变量是否有值,并且希望程序在没有值的情况下不会崩溃(所以不要使用!).就这样

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

这会给你一个空白或者值。

除了其他答案中提到的解决方案之外,如果你想在整个项目中始终避免使用可选文本,你可以添加这个 pod:

pod 'NoOptionalInterpolation'

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

Pod 添加了一个扩展以覆盖字符串插值 init 方法,从而一次性删除可选文本。它还提供了一个自定义操作符 * 来恢复默认行为。

所以:

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

有关详细信息,请参阅此答案 https://stackoverflow.com/a/37481627/6390582

试试这个,

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

如果你有信心在你的变量中没有空值,那么就先选择它。 此外,还可以使用 if let 或 Guard let 语句在不崩溃的情况下展开可选项。

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

守卫让,

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

swift3中,你可以轻松地删除可选项

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

Print (“ imageURLString =”+ imageURLString!)

用就是了!

你可以只是把 !和它将工作:

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


print(var1!) // "something"

简单地将 ?转换为 !就解决了我的问题:

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

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

虽然我们可能有不同的上下文,下面的工作对我来说。

我将变量的每个部分都包装在括号中,然后在右边的结束括号外添加一个叹号。

例如,print(documentData["mileage"])更改为:

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

你好,我得到了同样的问题,我得到了可选(3) 所以,我尝试了以下代码

文本 = “(数据? . 数量!)”//“可选(3)”

让数量 = 数据?

文本 = “(数量!)”//“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)

当您将任何变量定义为可选时,您需要打开该可选值!

如何删除可选的字符串字符,以便在 Swift UIkit 中进行打印

从这些(有可选打印)

print("Collection Cell \(categories[indexPath.row].title) \(indexPath.row)")

到这些(没有可选打印)

print("Collection Cell \(categories[indexPath.row].title ?? "default value") \(indexPath.row)")