Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Declaring Bitmasks:</h2> <p>Alternatively to assigning absolute values (<code>1</code>, <code>2</code>, <code>4</code>, …) you can declare <strong><a href="http://en.wikipedia.org/wiki/Bitmask" rel="noreferrer">bitmasks</a></strong> (how these are called) like this:</p> <pre><code>typedef enum : NSUInteger { FileNotDownloaded = (1 &lt;&lt; 0), // =&gt; 00000001 FileDownloading = (1 &lt;&lt; 1), // =&gt; 00000010 FileDownloaded = (1 &lt;&lt; 2) // =&gt; 00000100 } DownloadViewStatus; </code></pre> <p>or using modern ObjC's <code>NS_OPTIONS</code>/<code>NS_ENUM</code> macros:</p> <pre><code>typedef NS_OPTIONS(NSUInteger, DownloadViewStatus) { FileNotDownloaded = (1 &lt;&lt; 0), // =&gt; 00000001 FileDownloading = (1 &lt;&lt; 1), // =&gt; 00000010 FileDownloaded = (1 &lt;&lt; 2) // =&gt; 00000100 }; </code></pre> <p>(see <a href="https://stackoverflow.com/a/16168299/227536">Abizern's answer</a> for more info on the latter)</p> <p>The concept of bitmasks is to (usually) define each enum value with a single bit set.</p> <p>Hence <code>OR</code>ing two values does the following:</p> <pre><code>DownloadViewStatus status = FileNotDownloaded | FileDownloaded; // =&gt; 00000101 </code></pre> <p>which is equivalent to:</p> <pre><code> 00000001 // FileNotDownloaded | 00000100 // FileDownloaded ---------- = 00000101 // (FileNotDownloaded | FileDownloaded) </code></pre> <h2>Comparing Bitmasks:</h2> <p>One thing to keep in mind when checking against bitmasks:</p> <h3>Checking for exact equality:</h3> <p>Let's assume that status is initialized like this:</p> <pre><code>DownloadViewStatus status = FileNotDownloaded | FileDownloaded; // =&gt; 00000101 </code></pre> <p>If you want to check if <code>status</code> <strong>equals</strong> <code>FileNotDownloaded</code>, you can use:</p> <pre><code>BOOL equals = (status == FileNotDownloaded); // =&gt; false </code></pre> <p>which is equivalent to:</p> <pre><code> 00000101 // (FileNotDownloaded | FileDownloaded) == 00000100 // FileDownloaded ----------- = 00000000 // false </code></pre> <h3>Checking for "membership":</h3> <p>If you want to check if <code>status</code> merely <strong>contains</strong> <code>FileNotDownloaded</code>, you need to use:</p> <pre><code>BOOL contains = (status &amp; FileNotDownloaded) != 0; // =&gt; true 00000101 // (FileNotDownloaded | FileDownloaded) &amp; 00000100 // FileDownloaded ----------- = 00000100 // FileDownloaded != 00000000 // 0 ----------- = 00000001 // 1 =&gt; true </code></pre> <p>See the subtle difference (and why your current "if"-expression is probably wrong)?</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