Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

objective c - Inline Block With Return Type

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...