展开可选值
Unwrapping optional Value
我正在尝试解析其中包含这些值的可选值
Optional(UITableViewCell: ox7ff2f9cfbc80; frame = (0 0; 414 44); text = 'Clarity'; autoresize = W; layer = <CALayer:0x7ff2f9cfb250>>)
并且只想抓取 'Clarity' 的文本部分以在另一行中打印出来。请让我知道这是否可行,因为我是 Swift 的新手!谢谢!
这是我如何创建 table 个单元格
我这里有一个歌曲列表
tracks = [Clarity, Freak-a-Leek, What's My Age Again?, All The Small Things, Bandz A Make Her Dance, Cant Tell Me Nothing, Slow Jamz, Hate It Or Love It, Dark Horse, Teenage Dream, In Too Deep, Just Hold On, We're Going Home, Energy, Fat Lip]
并通过执行以下操作创建 tableviewcells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
cell?.textLabel?.text=self.tracks[indexPath.row]
return cell!
}
下面是我如何获得可选值
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
let Cell = tableView.cellForRowAtIndexPath(indexPath)
}
您可以使用可选链接进入可选以获取特定字段,然后检查链是否具有 if let
:
的值
if let txt = optionalCell?.text {
println(txt)
}
// if you want to, add an else
else {
// to handle the optional chain being nil
}
optionalCell?.text
表示:如果optionalCell
有一个值,得到text
属性,否则returnnil
。然后 if let
“展开”可选值,如果可选值包含一个值,则将 txt
设置为常规值。如果您希望代码处理不存在的情况,您可以添加一个 else
子句。
如果您想在 nil
的情况下使用默认值(例如,一个空字符串),那么这里有一个 shorthand:
let txt = optionalCell?.text ?? "Blank"
??
在左侧采用可选值,在右侧采用默认值,计算结果为可选值或默认值(如果为 nil
)。
您有时可能会看到有人推荐使用 !
。 不要听从他们的建议。 !
是一个“force-unwrap”,如果你解包的可选选项曾经是 nil
你的程序将退出一个断言。 !
有合法用途,但非常罕见,更常见的是人们在有更好的解决方案可用时错误推荐它
我正在尝试解析其中包含这些值的可选值
Optional(UITableViewCell: ox7ff2f9cfbc80; frame = (0 0; 414 44); text = 'Clarity'; autoresize = W; layer = <CALayer:0x7ff2f9cfb250>>)
并且只想抓取 'Clarity' 的文本部分以在另一行中打印出来。请让我知道这是否可行,因为我是 Swift 的新手!谢谢!
这是我如何创建 table 个单元格
我这里有一个歌曲列表
tracks = [Clarity, Freak-a-Leek, What's My Age Again?, All The Small Things, Bandz A Make Her Dance, Cant Tell Me Nothing, Slow Jamz, Hate It Or Love It, Dark Horse, Teenage Dream, In Too Deep, Just Hold On, We're Going Home, Energy, Fat Lip]
并通过执行以下操作创建 tableviewcells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
cell?.textLabel?.text=self.tracks[indexPath.row]
return cell!
}
下面是我如何获得可选值
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
let Cell = tableView.cellForRowAtIndexPath(indexPath)
}
您可以使用可选链接进入可选以获取特定字段,然后检查链是否具有 if let
:
if let txt = optionalCell?.text {
println(txt)
}
// if you want to, add an else
else {
// to handle the optional chain being nil
}
optionalCell?.text
表示:如果optionalCell
有一个值,得到text
属性,否则returnnil
。然后 if let
“展开”可选值,如果可选值包含一个值,则将 txt
设置为常规值。如果您希望代码处理不存在的情况,您可以添加一个 else
子句。
如果您想在 nil
的情况下使用默认值(例如,一个空字符串),那么这里有一个 shorthand:
let txt = optionalCell?.text ?? "Blank"
??
在左侧采用可选值,在右侧采用默认值,计算结果为可选值或默认值(如果为 nil
)。
您有时可能会看到有人推荐使用 !
。 不要听从他们的建议。 !
是一个“force-unwrap”,如果你解包的可选选项曾经是 nil
你的程序将退出一个断言。 !
有合法用途,但非常罕见,更常见的是人们在有更好的解决方案可用时错误推荐它