Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, it's not obvious what you are trying to do here.</p> <p>There are two possibilities. Perhaps you need to pass a parameter to the block. So there is a variable of type <code>bool</code> or <code>BOOL</code> (they are similar for the purposes of this question) and you want to use it in the block.</p> <p>Then, good news! You don't need anything at all. Just declare</p> <pre><code> BOOL some_bool = (YES or NO or some calculation); </code></pre> <p>and use it later in the block. This works because Boolean types are primitive (they are some kind of integer) and not smart enough to participate in memory management. So the compiler doesn't worry much; it just grabs the value of this variable when the block is created and copies it into the block.</p> <p>Here's <a href="https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html" rel="nofollow">the relevant documentation</a>: </p> <blockquote> <p>Only the value is captured, unless you specify otherwise. This means that if you change the external value of the variable between the time you define the block and the time it’s invoked, ...</p> </blockquote> <p>Alternatively, perhaps you want to have a variable which can take logical values and which can be changed by some other object in the time between you create the block and the time when it is run. In that case you need to</p> <ol> <li>wrap boolean into <code>NSNumber</code> </li> <li>store the strong reference to this <code>NSNumber</code> somewhere in your object</li> <li>pass it as a weak pointer to the block</li> </ol> <p>E.g.</p> <pre><code>// somewhere in the interface @property NSNumber *someImportantFlag __weak NSNumber *weakFlag = someImportantFlag; ... ^{ ... if(weakFlag.boolValue) ... weakFlag = @(NO); ...} </code></pre> <p>Thanks to the commenters who made me reread the question and my answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload