标签: optional-binding

条件绑定:如果让错误 - 条件绑定的初始化程序必须具有可选类型

我试图从我的数据源和以下代码行中删除一行:

if let tv = tableView {
Run Code Online (Sandbox Code Playgroud)

导致以下错误:

条件绑定的初始化程序必须具有Optional类型,而不是UITableView

这是完整的代码:

// Override to support editing the table view.
func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {

        // Delete the row from the data source

    if let tv = tableView {

            myData.removeAtIndex(indexPath.row)

            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
Run Code Online (Sandbox Code Playgroud)

我该如何纠正以下问题?

 if let tv = tableView {
Run Code Online (Sandbox Code Playgroud)

ios swift optional-binding

107
推荐指数
5
解决办法
14万
查看次数

如何在Swift 2中使用可选绑定

我是学习Swift的新手,所以我决定不再学习Swift 2.到目前为止,除了以下代码片段之外,一切对我都有意义.希望有人可以为我阐明这一点.

//: Playground - noun: a place where people can play
import UIKit

//Works
let possibleNumber="2"
if let actualNumber = Int(possibleNumber) {
    print("\'\(possibleNumber)\' has an integer value of \(actualNumber)")
}
else {
    print("could not be converted to integer")
}

//Doesn't Work and I'm not sure why
let testTextField = UITextField()
testTextField.text = "2"
let numberString = testTextField.text  //I know this is redundant

if let num = Int(numberString) {
    print("The number is: \(num)")
}
else {
    print("Could not be converted …
Run Code Online (Sandbox Code Playgroud)

optional uitextfield ios swift2 optional-binding

5
推荐指数
1
解决办法
2622
查看次数

下划线符号是否忽略或检查Swift中switch语句中的null?

我在Swift中有一个switch语句,如下所示:

switch tuple {
    case (let someObject, let current, nil):
        return true

    // Other cases...
}
Run Code Online (Sandbox Code Playgroud)

元组的类型的(SomeObject?, SomeObject, SomeObject?),和我说的英语是:匹配其中前两个元素的情况下为零,而第三个(可选的)零.

Xcode的7告诉我,因为我没有使用绑定someObjectcurrent,我应该用下划线代替它.但是如果我用下划线替换元组中的第一个元素,它是否也会匹配第一个元素为nil的情况,因为这_意味着编译器会忽略该值?对于第一个元素为零的情况,我有一个单独的案例.

为了记录,看起来我的代码仍然按照我的预期工作,但我想确定,我无法在任何地方找到任何文档.

switch-statement swift optional-binding

3
推荐指数
1
解决办法
450
查看次数

仅当一个表达式中的可选项不为 nil 时才调用函数?

我知道可以这样做:

let intValue: Int? = rawValue == nil ? Int(rawValue) : nil
Run Code Online (Sandbox Code Playgroud)

或者甚至像这样:

var intValue: Int?

if let unwrappedRawValue = rawValue {
    intValue = Int(unwrappedRawValue)
}
Run Code Online (Sandbox Code Playgroud)

不过我想知道是否有一种方法可以在一个表达式中做到这一点,如下所示:

let intValue: Int? = Int(rawValue) // Where Int() is called only if rawValue is not nil
Run Code Online (Sandbox Code Playgroud)

swift optional-binding option-type

3
推荐指数
1
解决办法
1202
查看次数

使用隐式解包的选项但是测试nil vs可选绑定是否有技术上的缺点?

好的,所以我知道在Swift中使用选项的正常方法是通过可选的绑定来解开它们,就像这样......

let stringA:String? = nil // (or "ABC")

if let unwrappedStringA = stringA
{
    let x:String = unwrappedStringA
}
Run Code Online (Sandbox Code Playgroud)

但是我也看到了使用隐式解包的选项的以下方式,我个人认为它看起来更干净,因为它不仅不需要额外的变量,而且它"读取"更好,更像英语(即'如果这是不是,然后......')可读性是Swift的核心原则之一(特别是在即将推出的Swift 3.0中).

let stringA:String! = nil // (or "ABC")

if stringA != nil
{
    let x:String = stringA
}
Run Code Online (Sandbox Code Playgroud)

然而,就后者而言,斯威夫特的"纯粹主义者"将此称为"代码嗅觉"并坚持认为它是"坏,坏,坏!!"......但他们从未解释过为什么!那么......为什么这么糟糕?

注意:是的,我知道可选链接和其他这些功能,你不能使用隐式解包的选项,它们都是非常酷的功能,但我特别询问测试隐式解包的选项对nil的技术缺点vs可选绑定.

我希望的是可量化的技术原因,为什么一个比另一个好(即编译器优化,更好的安全检查,性能等)换句话说,不仅仅是'嘿,因为那不是'Swifty'的方式去做吧!' 希望有道理.

更新

我实际上刚刚找到了一种方法来解决我的一个问题(至少是表面的方式),它必须创建一个新变量来保存未包装的变量.这里的技巧是因为展开的变量实际上与可选的本身不同,你可以使用完全相同的名称.

此外,括号内部还有另一个范围,它也可以有一个名为stringA的变量.这意味着你现在可以拥有三个'stringA'变量(但并不意味着你应该!)......

  1. 可选的
  2. 打开的可选项
  3. 括号内的新的局部变量

这是这个疯狂的代码显示这个......

let stringA:String? = nil // (or "ABC") // First stringA variable

if let stringA = stringA
{
    let x:String = stringA // <- This stringA is the unwrapped optional …
Run Code Online (Sandbox Code Playgroud)

optional swift optional-binding

3
推荐指数
2
解决办法
453
查看次数

为什么使用可选绑定?

我知道这个问题在这里,但它只是部分回答了我的问题,我无法对答案发表评论,所以我不得不在这里发帖.

可选绑定和只是简单地使用?有什么区别?我的理解是你何时使用?要解包变量,如果它包含nil值,则不运行使用它的代码.(如果不是这样,请纠正我.)

swift optional-binding

2
推荐指数
1
解决办法
986
查看次数

是否可以在同一条件语句中使用来自可选绑定的变量?

if let popupButton = result?.control as? NSPopUpButto {
    if popupButton.numberOfItems <= 1 {
        // blahblah
    }
}
Run Code Online (Sandbox Code Playgroud)

我想避免双重嵌套如果。

if let popupButton = result?.control as? NSPopUpButton && popupButton.numberOfItems <= 1 {}
Run Code Online (Sandbox Code Playgroud)

但是unresolved identifier如果我这样做,我会得到编译器错误。

有没有办法在一行上实现这个条件?或者因为我使用的是可选绑定,我是否被迫在if此处进行嵌套?

if-statement swift optional-binding

2
推荐指数
1
解决办法
60
查看次数

可选绑定的命名约定

最初阻止我在代码中加入过多可选绑定的一件事是添加了更多变量名称.例如,我通常会写:

if bananasInBarrel != nil{
  print("We have \(bananasInBarrel!) bananas in the barrel.")
}
Run Code Online (Sandbox Code Playgroud)

因为替代方案似乎有点乱:

if let safeBananas = bananasInBarrel{
  print("We have \(safeBananas) bananas in the barrel.")
}
Run Code Online (Sandbox Code Playgroud)

那是很多香蕉.我见过人们使用的东西就像b新的变量名(在较大的代码块中难以阅读),但我想知道是否有一个普遍接受的变量名称样式标准可供使用可选绑定?谢谢阅读.

optional swift optional-binding

1
推荐指数
1
解决办法
573
查看次数

Swift可选绑定,为什么需要本地var?

我正在阅读关于Optional Binding的 Apple开发人员文档

为什么我不能使用:

如果有些可选吗?{

声明

}

代替

如果让constantName = someOptional {

声明

}

为什么不需要局部变量或常数?

swift optional-binding

1
推荐指数
1
解决办法
240
查看次数

子模块的故障安全注入器。google guice 中的可选绑定

我有一个名为 ChildPlugin 的子模块,我从主模块注入类,如下所示:

public class ChildPlugin {
    private ExampleClass demo;

    @Inject
    public void setDemo(ExampleClass demo) { 
        this.demo = demo;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道主模块是否绑定ExampleClass,如果不是 Guice 在创建注入器时抛出异常。我想要做的是让 Guice 通过null或者Optional.empty如果 ExampleClass 没有绑定。

我没有进入主模块,所以我不能改变粘结剂ExampleClassOptionalBinder,我试图@NullableOptional<ExampleClass>ChildPlugin.setDemo方法,但没有奏效。

java dependency-injection guice optional-binding

0
推荐指数
1
解决办法
2371
查看次数