Are Swift / assigned functions assigned by value or by reference?

When a function in Swift is assigned to a variable or returned as a nested function from a higher-level function, is it passed by value or by reference? When I write:

func foo() -> Bool
{
    return false
}

var funcVar = foo

does it have a funcVarlink to foo()or is a copy foo()created and stored in memory with the name "funcVar"? The same question for the following code:

func otherfoo() -> (Int) -> ()
{
    func bar(num :Int) {}
    return bar
}

var funcVar = otherfoo()

, , , , funcVar(3), bar , / , bar scope funcVar, - ( ++, ). - ?

+4
3

Apple:

incrementBySeven incrementByTen , , , runningTotal, . , .

, , .

+6

- swift. .

func countAdder(_ word: String)-> () -> (){
var count = 0
func currentCount(){
    count += 1
    print(count)
}
return currentCount
}
let countedWord = countAdder("Hello")
let countedWordTwo = countAdder("World")
countedWord() // count is 1
countedWordTwo() // count is  1
//Lets try this

let countedWord = countAdder("Hello")
let countedWordTwo = countedWord
countedWord() // count is  1
countedWordTwo() // now count is 2

, .

+1

, Swift , Swift , ref, & . ( ref, & , .) .

, . , , (, structs) (, ), . , Swift ( "" , ), , , . .

, , , , funcVar (3) / , funcVar

I do not understand you. Functions are first-class citizens. So, as soon as you have the value of the function, you can use it, you can pass it, you can return it, like everything else, and the one who receives this function can use it like any other function. Whether the function value is the type of value that is being copied, or the reference to the object with reference counting does not matter.

0
source

All Articles