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

Categories

0 votes
2.0k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...