swift 如何删除可选的字符串字符 [英] swift How to remove optional String Character

查看:58
本文介绍了swift 如何删除可选的字符串字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除可选字符

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"

我该怎么办?

嗯……

推荐答案

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

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"

这篇关于swift 如何删除可选的字符串字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆