Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Copied verbatim from <a href="http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and" rel="noreferrer">http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and</a>:</p> <p><strong><em>Objective-C literals:</strong> one can now create literals for NSArray, NSDictionary, and NSNumber (just like one can create literals for NSString)</em></p> <h2>NSArray Literals</h2> <p>Previously:</p> <pre class="lang-m prettyprint-override"><code>array = [NSArray arrayWithObjects:a, b, c, nil]; </code></pre> <p>Now:</p> <pre class="lang-m prettyprint-override"><code>array = @[ a, b, c ]; </code></pre> <h2>NSDictionary Literals</h2> <p>Previously:</p> <pre class="lang-m prettyprint-override"><code>dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3] forKeys:@[k1, k2, k3]]; </code></pre> <p>Now:</p> <pre class="lang-m prettyprint-override"><code>dict = @{ k1 : o1, k2 : o2, k3 : o3 }; </code></pre> <h2>NSNumber Literals</h2> <p>Previously:</p> <pre class="lang-m prettyprint-override"><code>NSNumber *number; number = [NSNumber numberWithChar:'X']; number = [NSNumber numberWithInt:12345]; number = [NSNumber numberWithUnsignedLong:12345ul]; number = [NSNumber numberWithLongLong:12345ll]; number = [NSNumber numberWithFloat:123.45f]; number = [NSNumber numberWithDouble:123.45]; number = [NSNumber numberWithBool:YES]; </code></pre> <p>Now:</p> <pre class="lang-m prettyprint-override"><code>NSNumber *number; number = @'X'; number = @12345; number = @12345ul; number = @12345ll; number = @123.45f; number = @123.45; number = @YES; </code></pre> <hr> <p><strong>[Edit]</strong></p> <p><em>zxoq</em> at <a href="http://news.ycombinator.com/item?id=3672744" rel="noreferrer">http://news.ycombinator.com/item?id=3672744</a> has added more interesting new subscripting. (Added with literals):</p> <pre class="lang-m prettyprint-override"><code>arr[1] === [arr objectAtIndex:1] dict[@"key"] === [dict objectForKey:@"key"] </code></pre> <hr> <p><strong>[Edit 2]</strong></p> <p>The new ObjC literals were discussed in multiple <a href="http://developer.apple.com/videos/wwdc/2012/" rel="noreferrer">WWDC 2012</a> sessions. I intentionally didn't remove the the filenames and the time of each slide so you can find them for yourself if you feel like. They are essentially the same thing as stated in this post, but there are also a few new things that I'll mention above the images.</p> <p><em>Please note that images are all big. Simply drag them into another tab to view them in their original size</em></p> <p><img src="https://i.stack.imgur.com/n7DxW.png" alt="Literals &amp; Boxing"></p> <pre class="lang-m prettyprint-override"><code>[NSNumber numberWithint:42] [NSNumber numberWithDouble:10.8] [NSNumber numberWithBool:YES] [NSNumber numberWithint:6 + x * 2012] </code></pre> <p><img src="https://i.stack.imgur.com/RZ172.png" alt="Literals &amp; Boxing"></p> <pre class="lang-m prettyprint-override"><code>@42 @10.8 @YES @(6 + x * 2012) </code></pre> <hr> <p><img src="https://i.stack.imgur.com/g3GWT.png" alt="Collection Subscripting"></p> <pre class="lang-m prettyprint-override"><code>[NSArray arrayWithObjects: a, b, c, nil] [array objectAtIndex:i] [NSDictionary dictionaryWithObjectsAndKeys: v1, k1, v2, k2, nil]; [dictionary valueForKey:k] </code></pre> <p><img src="https://i.stack.imgur.com/T0dj3.png" alt="Collection Subscripting"></p> <pre class="lang-m prettyprint-override"><code>@[a, b, c] array[i] @{k1:v1, k2:v2} dictionary[k] </code></pre> <hr> <p><img src="https://i.stack.imgur.com/dE7P7.png" alt="@# numbers, @{} dictionaries, @&quot;&quot; strings, @[] arrays, @() expressions"></p> <hr> <h3>This part is new. <strong>Expression Literals</strong></h3> <p>When you have an expression (<code>M_PI / 16</code> for example) you should put it inside parenthesis.</p> <p>This syntax works for numeral expressions, booleans, finding an index in a (C-) string, boolean values, enum constants, and even character strings!</p> <p><img src="https://i.stack.imgur.com/SPYTD.png" alt="Expression Literals"></p> <pre class="lang-m prettyprint-override"><code>NSNumber *piOverSixteen = [NSNumber numberWithDouble: (M_PI / 16)]; NSNumber *hexDigit = [NSNumber numberWithChar:"0123456789ABCDEF"[i % 16]]; NSNumber *usesScreenFonts = [NSNumber numberWithBool:[NSLayoutManager usesScreenFonts]]; NSNumber *writingDirection = [NSNumber numberWithInt:NSWritingDirectionLeftToRight]; NSNumber *path = [NSString stringWithUTF8String: getenv("PATH")]; </code></pre> <p><img src="https://i.stack.imgur.com/P3CF7.png" alt="Expression Literals"></p> <pre class="lang-m prettyprint-override"><code>NSNumber *piOverSixteen = @( M_PI / 16 ); NSNumber *hexDigit = @( "0123456789ABCDEF"[i % 16] ); NSNumber *usesScreenFonts = @( [NSLayoutManager usesScreenFonts] ); NSNumber *writingDirection = @( NSWritingDirectionLeftToRight ); NSNumber *path = @( getenv("PATH") ); </code></pre> <hr> <p>More about character strings and how/when you can use this literal syntax:</p> <p><img src="https://i.stack.imgur.com/uXxf8.png" alt="Boxed String Expressions"></p> <pre class="lang-m prettyprint-override"><code>NSString *path = [NSString stringWithUTF8String: getenv("PATH")]; for (NSString *dir in [path componentsSeparatedByString: @":"]) { // search for a file in dir... } </code></pre> <p><img src="https://i.stack.imgur.com/iE6lT.png" alt="Boxed String Expressions"></p> <pre class="lang-m prettyprint-override"><code>NSString *path = @( getenv("PATH") ); for (NSString *dir in [path componentsSeparatedByString: @":"]) { // search for a file in dir... } </code></pre> <hr> <h3>How array literals work</h3> <p><img src="https://i.stack.imgur.com/B0tEh.png" alt="How array literals work"></p> <pre class="lang-m prettyprint-override"><code>// when you write this: array = @[a, b, c ]; // compiler generates: id objects[] = { a, b, c }; NSUInteger count = sizeof(objects) / sizeof(id); array = [NSArray arrayWithObjects:objects count:count]; </code></pre> <hr> <h3>How dictionary literals work</h3> <p><img src="https://i.stack.imgur.com/0kDy4.png" alt="How dictionary literals work"></p> <pre class="lang-m prettyprint-override"><code>// when you write this: dict = @{k1 : o1, k2 : o2, k3 : o3 }; // compiler generates: id objects[] = { o1, o2, o3 }; id keys[] = { k1, k2, k3 }; NSUInteger count = sizeof(objects) / sizeof(id); dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; </code></pre> <hr> <h3>More on array subscripting</h3> <p><img src="https://i.stack.imgur.com/j8Y4j.png" alt="Array Subscripting"></p> <pre class="lang-m prettyprint-override"><code>@implementation SongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atindex:(NSUinteger)idx { Song *oldSong = [_songs objectAtIndex:idx]; [_songs replaceObjectAtindex:idx withObject:newSong]; return oldSong; } </code></pre> <p><img src="https://i.stack.imgur.com/Di1jR.png" alt="Array Subscripting"></p> <pre class="lang-m prettyprint-override"><code>@implementation SongList { NSMutableArray *_songs; } - (Song *)replaceSong:(Song *)newSong atindex:(NSUinteger)idx { Song *oldSong = _songs[idx]; _songs[idx] = newSong; return oldSong; } </code></pre> <hr> <h3>More on dictionary subscripting</h3> <p><img src="https://i.stack.imgur.com/riY8s.png" alt="Dictionary Subscripting"></p> <pre class="lang-m prettyprint-override"><code>@implementation Database { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id &lt;NSCopying&gt;)key { id oldObject = [_storage objectForKey:key]; [_storage setObject:object forKey:key]; return oldObject; } </code></pre> <p><img src="https://i.stack.imgur.com/BX1i2.png" alt="Dictionary Subscripting"></p> <pre class="lang-m prettyprint-override"><code>@implementation Database { NSMutableDictionary *_storage; } - (id)replaceObject:(id)newObject forKey:(id &lt;NSCopying&gt;)key { id oldObject = _storage[key]; _storage[key] = newObject; return oldObject; } </code></pre> <hr> <p><strong>[Edit 3]</strong></p> <p><a href="http://www.mikeash.com/" rel="noreferrer">Mike Ash</a> has a great writeup about these new literals. If you want to know more about this stuff, make sure to <a href="http://www.mikeash.com/pyblog/friday-qa-2012-06-22-objective-c-literals.html" rel="noreferrer">check it out</a>.</p> <hr>
    singulars
    1. This table or related slice is empty.
    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