Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 区别于\(字符串)“;还有绳子?_Swift - Fatal编程技术网

Swift 区别于\(字符串)“;还有绳子?

Swift 区别于\(字符串)“;还有绳子?,swift,Swift,我用相同的字符串值调用相同的函数,但方法不同。。。。 让我知道有什么区别吗?我还想知道内存消耗。没有区别,如果字符串是 callfunc(string: "\(string)") callfunc(string: string) i、 e.首先不是字符串 没有内存差异,因为Swift中的String不是引用类型,而是Struct,所以您将String的副本传递给callfunc,而不是引用它。没有区别,如果字符串类似于 callfunc(string: "\(stri

我用相同的字符串值调用相同的函数,但方法不同。。。。
让我知道有什么区别吗?我还想知道内存消耗。

没有区别,如果字符串是

    callfunc(string: "\(string)")

    callfunc(string: string)
i、 e.首先不是
字符串


没有内存差异,因为
Swift
中的
String
不是引用类型,而是
Struct
,所以您将
String
的副本传递给
callfunc
,而不是引用它。

没有区别,如果字符串类似于

    callfunc(string: "\(string)")

    callfunc(string: string)
i、 e.首先不是
字符串


没有内存差异,因为
Swift
中的
String
不是引用类型,而是
Struct
,所以您将
String
的副本传递给
callfunc
,而不是引用它。

当字符串隐式展开时会有差异。考虑例子:

let someInt: Int = 20
print("my integer is \(someInt)") //"my integer is 20"
输出将是:

func some(string: String)
{
    print(string)
}

let string: String! = "s"

some(string: string)
some(string: "\(string)")

如果字符串是隐式展开可选的,则会有所不同。考虑例子:

let someInt: Int = 20
print("my integer is \(someInt)") //"my integer is 20"
输出将是:

func some(string: String)
{
    print(string)
}

let string: String! = "s"

some(string: string)
some(string: "\(string)")
callfunc(字符串:字符串)

在上面的语法中,它是一个带有字符串的普通函数调用

callfunc(字符串:“(字符串)”)

但在这里,当我们将“(string)”作为参数传递时,内部“(string)”将创建一个新字符串并将其作为参数传递。因此,在特定的时间点,由于字符串的内存分配,内存将变高,而字符串将再次立即释放

通常情况下,您不能用一个小字符串来观察它,但如果您将图像转换为base64并尝试将其作为字符串传递,则可以。你可以看到区别

除此之外,功能上没有区别。

callfunc(string:string)

在上面的语法中,它是一个带有字符串的普通函数调用

callfunc(字符串:“(字符串)”)

但在这里,当我们将“(string)”作为参数传递时,内部“(string)”将创建一个新字符串并将其作为参数传递。因此,在特定的时间点,由于字符串的内存分配,内存将变高,而字符串将再次立即释放

通常情况下,您不能用一个小字符串来观察它,但如果您将图像转换为base64并尝试将其作为字符串传递,则可以。你可以看到区别


除此之外,在功能上没有区别。

如果您的字符串变量是隐式展开的可选变量,则有区别。在这种情况下,如果字符串变量是隐式展开可选的,则将“Optional(string)”作为字符串值传递是有区别的。在本例中,您将“可选(字符串)”作为字符串值传递