斯威夫特是否自动克隆参数? [英] Does Swift Automatically Clone Arguments?

查看:116
本文介绍了斯威夫特是否自动克隆参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读上斯威夫特发布了苹果的iBook,而我目前阅读功能。在本章中,他们讨论INOUT关键字。该使我怀疑:当它们被作为参数传入函数传递并迅速自动克隆参数呢?如果没有,我可以创建一个常数,等于我要传递到函数,然后该变量传递给它不断以避免变异的变量,如果我不想要? EX(顺便说一句,我知道整数是不可改变的。我只是使用整数保持简单。)

I have been reading the iBook Apple released on Swift, and I am currently reading about functions. In this chapter they discuss the inout keyword. The led me to wonder: does swift automatically clone arguments when they are passed as parameters into functions? If not, could I create a constant equal to the variable I want to pass into the function and then pass it the constant to avoid mutating the variable if I do not want to? ex (By the way, I know that integers are immutable. I am just using integers to keep it simple.)

var variable: Int = 5
let constant = variable
let returnVale = function(constant)

(很抱歉,如果这是一个明显的答案。我很少编程经验。)谢谢您的帮助。

(Sorry if this is an obvious answer. I have very little programming experience.) Thank you for any help.

推荐答案

默认情况下,值类型变量传递给不只是价值的方法和功能,但正如值常量。

By default, value type variables are passed to methods and functions not just by value, but as constants by value.

也就是说,不仅可以在功能不改变功能以外的变量的值,它甚至不能改变内部的功能的变量的值。

That is, not only can the function not change the value of the variable outside the function, it can't even change the value of the variable INSIDE the function.

为了一个函数来修改函数内部的值,函数必须声明参数作为 VAR 参数。这样做可以让函数修改变量的函数内部,然而这仍然不会影响函数外部变量。

In order for a function to modify the value inside the function, the function must declare the parameter as a var parameter. Doing this allows the function to modify the variable inside the function, however this still does not effect the variable outside the function.

只有当指定了 INOUT 可以一个函数修改函数外的变量。在这种情况下,我们通过引用传递变量。

Only when inout is specified can a function modified the variable outside the function. In this case, we've passed the variable by reference.

有关这个话题更多的阅读,<一个href=\"https://developer.apple.com/library/$p$prelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html\">here's链接到官方文档。如果向下滚动的方式约三分之二,你会后立即找到一个名为常量和变量参数,另一部分部分这个名为输入 - 输出参数。

For more reading on this topic, here's a link to the official documentation. If you scroll down about two-thirds of the way, you'll find a section called "Constant and Variable Parameters" and another section right after this called "In-Out Parameters".

您不必担心不知何时功能会或不会修改变量。这是pretty容易知道。当一个函数声明的参数作为 INOUT 参数,则需要在该参数的函数调用中使用运算符的地址。

You don't have to worry about knowing when a function will or won't modify your variable. It's pretty easy to know. When a function has declared a parameter as an inout parameter, you need to use the "address of" operator in the function call with that parameter.

这是从文档的功能,例如:

This is the example function from the documentation:

func swapTwoInts(inout a: Int, inout b: Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

现在,下面给出两个整数:

Now, given the following two Ints:

var someInt = 3
var anotherInt = 107

我们不能调用上面的函数这样:

We can not call the above function as such:

swapTwoInts(someInt, anotherInt)

编译器会抱怨:

传递类型'诠释'的价值,一个INOUT参数需要显式'和;

Passing value of type 'Int' to an inout parameter requires explicit '&'

要解决该编译器的投诉,我们必须使用取址运算符:

To fix the compiler complaint, we must use the address-of operator:

swapTwoInts(&someInt, &anotherInt)

现在的编译器是幸福的,而&安培; ,运营商地址的,提供给我们的,我们已经通过引用传递我们的变量一大红旗并且功能可以修改它们的值。

Now the compiler is happy, and that &, the address-of operator, serves to us as a big red flag that we've passed our variables by reference and the function could modify their values.

这篇关于斯威夫特是否自动克隆参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆