Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is it possible in Objective-C to create a block inline and use its return type? For example, can I create a block that returns a BOOL, have it be inline, and use its return type for an assignment.

BOOL b = <inline block that returns BOOL> { // unsure of the syntax / legality of this
    return YES; // will be more logic in here, that is why I want to use a block.
};

I am having trouble with the block syntax, and am unsure if a block can be created inline. I have checked the following resources with no avail.

Thankyou for your time and patience if this happens to be impossible, or very easy.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

A different way to achieve that result is a "compound statement expression":

BOOL b = ({
    BOOL result;
    // other local variables and stuff, computing "result"
    result; // The last expression is the value of this compound statement expression. 
});

This is a GCC extension to the C language (and understood by Clang as well). It looks similar to a block, but is something different.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

56.5k users

...