The differential between closure and function as an argument in fast

I have almost 4 years experience working with Objective-C and new to the fast version. I am trying to understand the concept of fast from the point of view of Objective C. So if I am wrong, please help me through :)

In object c, we have blocks (code chunck, which can be executed asynchronously later), which made absolutely perfect sense. But soon we can pass the function as a parameter to another function that can be executed later, and then we also close it.

According to Apple, "features are special cases of offers."

According to O'Reilly, "when a function is passed as a value, it wraps internal references to external variables, which makes the function a closure."

So, I tried to understand a little the same thing :)

Here is my closing

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a ni

        let tempNumber : Int = 5

        let numbers = [1,2,3,4,5]
        print (numbers.map({ $0 - tempNumber}))
}

The tempNumber variable is declared before a close has been declared, but the close has access to the variable. Now, and not a map, I tried to use a special class that passed closure as a parameter, and tried to execute the same code :) Although now closure is performed in a different area, it still has access to tempNumber.

I came to the conclusion: closure has access to variables and methods that are declared in the same scope as closure, but it is executed in a different scope.

, , paramter, ,

class test {
    func testFunctionAsParameter(testMethod : (Int) -> Int){
        let seconds = 4.0
        let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

        dispatch_after(dispatchTime, dispatch_get_main_queue(), {
             self.callLater(testMethod)
        })
    }

    func callLater(testMethod : (Int) -> Int) -> Int {
        return testMethod(100)
    }
}

differrent Test ,

/* in differrent class */
    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a ni

            let tempAge : Int = 5

            func test2(val : Int) -> Int {
                return val - tempAge;
            }

            let testObj = test();
            print(testObj.testFunctionAsParameter(test2))
        }

test, testFunctionAsParameter, , callLater, , , :)

, , :)

:), , , , , , , , testNumber, , :)

: ' " , ". :)

, , - . , - :) , , , , !!!!

, differrence , ??? - , Apple :)

? ? ' , " , , ". ? ? , ?

Apple O'Reilly: (, , - ?? , .

+4
2

. - ( .) , , .

Objective-C / .

+5

, , , . :

1. a closure is a closure
2. a function is a closure with a name

. , . "" .

?

+5

All Articles