Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:44:37+00:00 2026-06-13T19:44:37+00:00

In the Apple block documentation is an example of code not to write: void

  • 0

In the Apple block documentation is an example of code not to write:

void dontDoThisEither() {
  void (^block) (void);
  int i = random();
  if (i > 1000) {
    block = ^{printf("got i at: %d\n", i); };
  }
  // ...
}

The comments for the code say the block literal scope is the “then” clause.
I don’t understand what they mean by that, there is no then clause, which is presumably why they have put it in quotes. But why have they put it in quotes and what is the relationship to the block’s scope?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:44:38+00:00Added an answer on June 13, 2026 at 7:44 pm

    Think of an if statement as:
    if this then that else this other thing

    The {... block = ...} is in the then that part of the if statement. That is, it is *a sub-scope of the scope of the dontDoThisEither() function.

    Because blocks are created on the stack and are only valid within the scope of their declaration, that means that the block assignment in that example is only valid within the then that scope of the if statement.

    I.e. Consider:

    void dontDoThisEither() {
      void (^block) (void);
      int i = random();
      if (i > 1000) {
        block = ^{printf("got i at: %d\n", i); };
      }  else {
        block = ^{printf("your number is weak and small. ignored.\n");};
      }
      block();
    }
    

    At the time block(); is executed the block that it is pointing to is in a scope that is no longer valid and the behavior will be undefined (and likely crashy in a real world example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apple says about the UITableView -beginUpdates -endUpdates block: If you do not make the
For example, http://developer.apple.com/cocoa/pyobjc.html is still for OS X 10.4 Tiger, not 10.5 Leopard.. And
Apple.h class Apple { public: Apple(int); static int typeID; private: int id_; }; Apple.cpp
Apple really had bad documentation about how the provider connects and communicates to their
I've been studying the sample code Apple provides in their iPhoneCoreDataRecipes application and there
I took apple's sample code tweeting and modified it so that it doesn't use
I've got yet another error trying to compile with Apple GCC 4.2.1 using the
I have in my rails form the following code: <label>Fruit: </label> <%= f.select(:IST_FRUIT, [['Apple',
From the Apple documentation on animating property changes in a view, In order to
I'm interesting in creation of PushCertWebRequest (this is from apple documentation) I have found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.