标签: optional-variables

Swift中的低价选项:as?输入,或者!类型?

在Swift中给出以下内容:

var optionalString: String?
let dict = NSDictionary()
Run Code Online (Sandbox Code Playgroud)

以下两个陈述之间的实际区别是什么:

optionalString = dict.objectForKey("SomeKey") as? String
Run Code Online (Sandbox Code Playgroud)

VS

optionalString = dict.objectForKey("SomeKey") as! String?
Run Code Online (Sandbox Code Playgroud)

downcast optional optional-variables swift

91
推荐指数
5
解决办法
3万
查看次数

多个可选参数调用函数

假设我有一个如下所示的函数它需​​要3个参数,2个具有可选值

private void  myfunc (int a, int b=2, int c=3)
{
  //do some stuff here related to a,b,c
}
Run Code Online (Sandbox Code Playgroud)

现在我想调用这个函数如下可能吗?

myfunc(3,,5)
Run Code Online (Sandbox Code Playgroud)

所以我希望它使用默认参数b = 2

但它正是这样给出错误的.

这里是错误消息

Argument missing
Run Code Online (Sandbox Code Playgroud)

C#4.5

c# function optional-parameters optional-variables optional-arguments

24
推荐指数
2
解决办法
2万
查看次数

VB.NET:=运算符

以下是什么意思?

Class.Function(variable := 1 + 1)
Run Code Online (Sandbox Code Playgroud)

这个操作符叫什么,它做了什么?

vb.net optional-variables

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

联盟中的boost :: optional <>?

我有一个可选的POD结构,它将包含在一个union中.
boost::optional<>按价值保持其类型,所以我认为这可行:

union helper
{
    int foo;
    struct 
    {
        char basic_info;
        struct details {
            //...
        };

        boost::optional<details> extended_info;
    } bar;
    //  ...
};

helper x = make_bar();

if( x.bar.extended_info )
{
    // use x.bar.extended_info->elements
}
Run Code Online (Sandbox Code Playgroud)

但VS2008抱怨我的barstruct现在有一个复制构造函数,因为该boost::optional<details>元素.

作为替代,我添加了一个布尔标志来指示可选参数是否有效,但是它很笨重:

union helper
{
    int foo;
    struct 
    {
        char basic;
        struct details {
            bool valid;
            //...
        } extended;
    } bar;
    //  ...
};
Run Code Online (Sandbox Code Playgroud)

我考虑过实现details::operator bool()返回details::valid变量,但这是模糊不清的,也是对人类的伤害.
boost::optional<>清楚地记录语法和意图,不需要侦探工作.

最后,helperunion需要是POD,所以我不能做任何动态分配 - 否则我会使用指针.

对于在语法上与 …

c++ boost optional-variables

5
推荐指数
2
解决办法
994
查看次数

在C++中一般封装结构中的"可选"字段的最佳方法是什么?

我有许多具体结构,我想将字段指定为可选(现在或不存在).只是想知道人们有什么想法来实现这一目标.这是一个示例结构(字段也可以是其他结构,甚至是结构的向量):

 struct LogonMessage_t
       {
          Header_t header; // this points to another struct containing all primitives
          std::string username;
          std::string password;
          std::vector<LogonOption_t> LogonOptions;
          int subaccountid;
          std::string Text;
       }
Run Code Online (Sandbox Code Playgroud)

我想将所有字段默认为不存在并逐个启用它们,也许是在它们的setter中.由于生成了这些结构,因此优选通用方法.

我到目前为止的想法是:

  1. 指示字段是否已设置的字段位图
  2. 使用sentinel值(对于std :: string为"\ 0",对于int为-1​​,对于float为-1.0f)
  3. 某种模板容器/代理封装每个字段以指示它是否已设置,任何想法?我认为这可能是最好的方法.

顺便说一句,使用地图或其他STL容器来封装字段在这里不起作用,它们需要是具体的结构.

c++ templates struct optional-variables

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

在 Swift 中遍历视图控制器层次结构

我想遍历 Swift 中的视图控制器层次结构并找到一个特定的类。这是代码:

extension UIViewController{

    func traverseAndFindClass<T : UIViewController>() -> UIViewController?{

        var parentController = self.parentViewController as? T?
                                    ^
                                    |
                                    |
        // Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController'
        while(parentController != nil){

            parentController = parentController!.parentViewController

        }

        return parentController

    }

}
Run Code Online (Sandbox Code Playgroud)

现在,我知道 parentViewController 属性返回一个可选的 UIViewController,但我不知道如何以上帝的名义使 Generic 成为可选类型。也许使用某种 where 子句?

generics optional-variables ios swift

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

检查可选的isEqualToString

我有一个案例,我想检查一个可选的我是否等于一个字符串.首先,我必须打开它以检查它是否存在,然后我想检查它是否等于另一个字符串.然而,这给我一个问题,我必须两次编写相同的代码.我给你举个例子:

    var person : Person = Person()

    if let name = person.name {
        if name.isEqualToString("John") {
            println("Hello, John!")
        }
        else {
            println("Wait a minute, you're not John!")
    }
    else {
        println("Wait a minute, you're not John!")
    }
Run Code Online (Sandbox Code Playgroud)

如你所见,我必须写两次else语句.一旦,如果名称存在且不是'John',那么该名称不存在的另一个时间.

我的问题是,如何以正确的方式做到这一点.

谢谢你的帮助!

optional-variables swift

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

Swift可选的初始化

这是我的理解

var perhapsInt : Int?
Run Code Online (Sandbox Code Playgroud)

这会自动设置为一个.None值.以下代码片段确认(没有编译器错误)

class MyClass {
    var num1: Int = 0
    var num2: Int?

    init(num1: Int) {
        self.num1 = num1
    }
}

var newClass = MyClass(num1: 5)
newClass.num1 // prints '5'
newClass.num2 // prints 'nil'
Run Code Online (Sandbox Code Playgroud)

我对可选的初始化过程的理解是否正确?如果是这样,为什么当我换num2到时这不起作用let.

我期待nil使用时可选项默认的相同行为let.我在这里错过了什么吗?

class MyClass {
    var num1: Int = 0
    let num2: Int?

    init(num1: Int) {
        self.num1 = num1
        // compiler error : return from initialiser without initialising all stored properties 
    } …
Run Code Online (Sandbox Code Playgroud)

initialization init optional optional-variables swift

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

生成空属性的值

我试图填充使用OptionalSPARQL查询语言生成的空白空间.有什么办法可以实现这个目标吗?

使用!bound可选变量生成true或false,但我想用我自己的值填充单元格,例如"?" 或"未知".

sparql optional-variables

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

Swift 2:是否有一个简短的语法来有条件地从可选变量设置一个非可选变量?

有时我发现自己编写了swift 2代码,如下所示:

class Test {
    var optionalInt: Int?
    var nonOptionalInt: Int = 0
    func test() {
        if let i = optionalInt {
            nonOptionalInt = i
        }
        // do more stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

有问题的部分是这样的:

if let i = optionalInt {
    nonOptionalInt = i
}
Run Code Online (Sandbox Code Playgroud)

在单词中:如果optionalInt有值,则将其分配给变量nonOptionalInt,否则不执行任何操作.

在swift 2中是否有一个等效的方式来表达这个优雅的单行而不添加一个中间变量if if i?

编辑

在考虑了第一个答案之后......

显然有一种等效的方式

if optionalInt != nil {
    nonOptionalInt = optionalInt!
}
Run Code Online (Sandbox Code Playgroud)

三元运营商?:不是等价的,因为它们可能会触发原始代码没有的didSet(如果这是预期的副作用,那就好了)

到目前为止,最优雅的答案似乎是

nonOptionalInt = optionalInt ?? nonOptionalInt
Run Code Online (Sandbox Code Playgroud)

它也可能像三元运算符一样触发didSet,因此不等效(如果是这样的话也很好).

我想我对Apple的愿望就像是

nonOptionalInt = optionalInt ?? 
Run Code Online (Sandbox Code Playgroud)

要么

nonOptionalInt ??= optionalInt
Run Code Online (Sandbox Code Playgroud)

optional-variables swift2

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