문제

Can we write Continuation passing style code in Objective C? If yes, could you please give some examples?

도움이 되었습니까?

해결책

Continuation Passing Style, or CPS, is a style of programming using anonymous functions to replace return statements. Instead of returning a value, a function will take another function as a parameter. Then, when it gets to the point where it would have returned a value, it calls the passed-in function with the value as a parameter instead.

In Objective-C, we now have anonymous functions in the form of blocks, so CPS can be achieved using blocks.

Here's an example of how CPS looks. This is the standard-style code:

NSString *string = [obj stringWhatever];
    // use string

And here it is converted to Continuation Passing Style:

[obj stringWhatever: ^(NSString *string) {
    // use string
}];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top