小编Sed*_*dem的帖子

swift 中的 Nil Coalescing (??) 运算符如何工作?

我的游乐场代码

let nickName: String? = nil
let fullName: String = "John Appleseed"

let informalGreeting = "Hi \(nickName ?? fullName)"
Run Code Online (Sandbox Code Playgroud)

嗨,约翰

let informalGreeting2 = "Hi \(fullName ?? nickName)"
Run Code Online (Sandbox Code Playgroud)

嗨可选(“约翰·苹果籽”)


我以为我理解第一种情况

let informalGreeting = "Hi \(nickName ?? fullName)"
Run Code Online (Sandbox Code Playgroud)

nickName 是 nil,所以输出必须是 "Hi \(fullName)" => "Hi John Appleseed"

在第二种情况下

let informalGreeting2 = "Hi \(fullName ?? nickName)"

第一个值 fullName 不是 nil ,所以我认为输出应该是 "Hi \(fullName)" => "Hi John Appleseed"。

但是为什么输出是一个可选的包装输出,就像这里

嗨可选(“约翰·苹果籽”)

null null-coalescing-operator swift

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

为什么用指针表示法取消引用特定的数组索引是不可能的

我有两个不同的代码示例.在第一个代码中,可以取消引用:

void getValue(int *myPointer)
{
    *myPointer = 10000;             
    return;
}


int main()
{
    int get_the_value = 2;
    getValue(&get_the_value);


    printf("The value of get_the_value = %d\n", get_the_value);
    return 0;


}
Run Code Online (Sandbox Code Playgroud)

但是在下面的代码中,不可能在func()中取消引用*B. 我从互联网上下面有这个代码,他们说:

" B是一个指向int的指针,因此B [0]是一个int,你不能取消引用一个int. "

但是*myPointer在第一个代码中也不是int类型吗?

所以我的问题是:为什么解除引用在第一个代码中工作而在第二个代码中不起作用?

#include <stdio.h>

int func(int *B){
    *B[0] = 5;
}

int main(void){

    int B[3] = {1, 2, 3};
    printf("b[0] = %d\n\n", B[0]);
    func(&B);
    printf("b[0] = %d\n\n", B[0]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers dereference

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

为什么在Integer.class的官方Java文档中没有getChars(int i,int index,char [] buf)方法

当我在Eclipse中查找Integer.class文件时,我可以找到getChars方法:https://imgur.com/CPD68sA

但在官方Java文档中没有getChars方法:https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html

为什么?

java documentation integer

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