blob: 92e4fb65cd293277fde479754d40d96cccd8146a [file] [log] [blame]
Sean Silvaf3a1ff32012-12-21 00:17:02 +00001====================
2Objective-C Literals
3====================
4
5Introduction
6============
7
8Three new features were introduced into clang at the same time:
9*NSNumber Literals* provide a syntax for creating ``NSNumber`` from
10scalar literal expressions; *Collection Literals* provide a short-hand
11for creating arrays and dictionaries; *Object Subscripting* provides a
12way to use subscripting with Objective-C objects. Users of Apple
13compiler releases can use these features starting with the Apple LLVM
14Compiler 4.0. Users of open-source LLVM.org compiler releases can use
15these features starting with clang v3.1.
16
17These language additions simplify common Objective-C programming
18patterns, make programs more concise, and improve the safety of
19container creation.
20
21This document describes how the features are implemented in clang, and
22how to use them in your own programs.
23
24NSNumber Literals
25=================
26
27The framework class ``NSNumber`` is used to wrap scalar values inside
28objects: signed and unsigned integers (``char``, ``short``, ``int``,
29``long``, ``long long``), floating point numbers (``float``,
30``double``), and boolean values (``BOOL``, C++ ``bool``). Scalar values
31wrapped in objects are also known as *boxed* values.
32
33In Objective-C, any character, numeric or boolean literal prefixed with
34the ``'@'`` character will evaluate to a pointer to an ``NSNumber``
35object initialized with that value. C's type suffixes may be used to
36control the size of numeric literals.
37
38Examples
39--------
40
41The following program illustrates the rules for ``NSNumber`` literals:
42
43.. code-block:: objc
44
45 void main(int argc, const char *argv[]) {
46 // character literals.
47 NSNumber *theLetterZ = @'Z'; // equivalent to [NSNumber numberWithChar:'Z']
48
49 // integral literals.
50 NSNumber *fortyTwo = @42; // equivalent to [NSNumber numberWithInt:42]
51 NSNumber *fortyTwoUnsigned = @42U; // equivalent to [NSNumber numberWithUnsignedInt:42U]
52 NSNumber *fortyTwoLong = @42L; // equivalent to [NSNumber numberWithLong:42L]
53 NSNumber *fortyTwoLongLong = @42LL; // equivalent to [NSNumber numberWithLongLong:42LL]
54
55 // floating point literals.
56 NSNumber *piFloat = @3.141592654F; // equivalent to [NSNumber numberWithFloat:3.141592654F]
57 NSNumber *piDouble = @3.1415926535; // equivalent to [NSNumber numberWithDouble:3.1415926535]
58
59 // BOOL literals.
60 NSNumber *yesNumber = @YES; // equivalent to [NSNumber numberWithBool:YES]
61 NSNumber *noNumber = @NO; // equivalent to [NSNumber numberWithBool:NO]
62
63 #ifdef __cplusplus
64 NSNumber *trueNumber = @true; // equivalent to [NSNumber numberWithBool:(BOOL)true]
65 NSNumber *falseNumber = @false; // equivalent to [NSNumber numberWithBool:(BOOL)false]
66 #endif
67 }
68
69Discussion
70----------
71
72NSNumber literals only support literal scalar values after the ``'@'``.
73Consequently, ``@INT_MAX`` works, but ``@INT_MIN`` does not, because
74they are defined like this:
75
76.. code-block:: objc
77
78 #define INT_MAX 2147483647 /* max value for an int */
79 #define INT_MIN (-2147483647-1) /* min value for an int */
80
81The definition of ``INT_MIN`` is not a simple literal, but a
82parenthesized expression. Parenthesized expressions are supported using
83the `boxed expression <#objc_boxed_expressions>`_ syntax, which is
84described in the next section.
85
86Because ``NSNumber`` does not currently support wrapping ``long double``
87values, the use of a ``long double NSNumber`` literal (e.g.
88``@123.23L``) will be rejected by the compiler.
89
90Previously, the ``BOOL`` type was simply a typedef for ``signed char``,
91and ``YES`` and ``NO`` were macros that expand to ``(BOOL)1`` and
92``(BOOL)0`` respectively. To support ``@YES`` and ``@NO`` expressions,
93these macros are now defined using new language keywords in
94``&LT;objc/objc.h&GT;``:
95
96.. code-block:: objc
97
98 #if __has_feature(objc_bool)
99 #define YES __objc_yes
100 #define NO __objc_no
101 #else
102 #define YES ((BOOL)1)
103 #define NO ((BOOL)0)
104 #endif
105
106The compiler implicitly converts ``__objc_yes`` and ``__objc_no`` to
107``(BOOL)1`` and ``(BOOL)0``. The keywords are used to disambiguate
108``BOOL`` and integer literals.
109
110Objective-C++ also supports ``@true`` and ``@false`` expressions, which
111are equivalent to ``@YES`` and ``@NO``.
112
113Boxed Expressions
114=================
115
116Objective-C provides a new syntax for boxing C expressions:
117
118.. code-block:: objc
119
120 @( <expression> )
121
122Expressions of scalar (numeric, enumerated, BOOL) and C string pointer
123types are supported:
124
125.. code-block:: objc
126
127 // numbers.
128 NSNumber *smallestInt = @(-INT_MAX - 1); // [NSNumber numberWithInt:(-INT_MAX - 1)]
129 NSNumber *piOverTwo = @(M_PI / 2); // [NSNumber numberWithDouble:(M_PI / 2)]
130
131 // enumerated types.
132 typedef enum { Red, Green, Blue } Color;
133 NSNumber *favoriteColor = @(Green); // [NSNumber numberWithInt:((int)Green)]
134
135 // strings.
136 NSString *path = @(getenv("PATH")); // [NSString stringWithUTF8String:(getenv("PATH"))]
137 NSArray *pathComponents = [path componentsSeparatedByString:@":"];
138
139Boxed Enums
140-----------
141
142Cocoa frameworks frequently define constant values using *enums.*
143Although enum values are integral, they may not be used directly as
144boxed literals (this avoids conflicts with future ``'@'``-prefixed
145Objective-C keywords). Instead, an enum value must be placed inside a
146boxed expression. The following example demonstrates configuring an
147``AVAudioRecorder`` using a dictionary that contains a boxed enumeration
148value:
149
150.. code-block:: objc
151
152 enum {
153 AVAudioQualityMin = 0,
154 AVAudioQualityLow = 0x20,
155 AVAudioQualityMedium = 0x40,
156 AVAudioQualityHigh = 0x60,
157 AVAudioQualityMax = 0x7F
158 };
159
160 - (AVAudioRecorder *)recordToFile:(NSURL *)fileURL {
161 NSDictionary *settings = @{ AVEncoderAudioQualityKey : @(AVAudioQualityMax) };
162 return [[AVAudioRecorder alloc] initWithURL:fileURL settings:settings error:NULL];
163 }
164
165The expression ``@(AVAudioQualityMax)`` converts ``AVAudioQualityMax``
166to an integer type, and boxes the value accordingly. If the enum has a
Sean Silva159cc9e2013-01-02 13:07:47 +0000167:ref:`fixed underlying type <objc-fixed-enum>` as in:
Sean Silvaf3a1ff32012-12-21 00:17:02 +0000168
169.. code-block:: objc
170
171 typedef enum : unsigned char { Red, Green, Blue } Color;
172 NSNumber *red = @(Red), *green = @(Green), *blue = @(Blue); // => [NSNumber numberWithUnsignedChar:]
173
174then the fixed underlying type will be used to select the correct
175``NSNumber`` creation method.
176
177Boxing a value of enum type will result in a ``NSNumber`` pointer with a
178creation method according to the underlying type of the enum, which can
Sean Silva159cc9e2013-01-02 13:07:47 +0000179be a :ref:`fixed underlying type <objc-fixed-enum>`
Sean Silvaf3a1ff32012-12-21 00:17:02 +0000180or a compiler-defined integer type capable of representing the values of
181all the members of the enumeration:
182
183.. code-block:: objc
184
185 typedef enum : unsigned char { Red, Green, Blue } Color;
186 Color col = Red;
187 NSNumber *nsCol = @(col); // => [NSNumber numberWithUnsignedChar:]
188
189Boxed C Strings
190---------------
191
192A C string literal prefixed by the ``'@'`` token denotes an ``NSString``
193literal in the same way a numeric literal prefixed by the ``'@'`` token
194denotes an ``NSNumber`` literal. When the type of the parenthesized
195expression is ``(char *)`` or ``(const char *)``, the result of the
196boxed expression is a pointer to an ``NSString`` object containing
197equivalent character data, which is assumed to be '\\0'-terminated and
198UTF-8 encoded. The following example converts C-style command line
199arguments into ``NSString`` objects.
200
201.. code-block:: objc
202
203 // Partition command line arguments into positional and option arguments.
204 NSMutableArray *args = [NSMutableArray new];
205 NSMutableDictionary *options = [NSMutableDictionary new];
206 while (--argc) {
207 const char *arg = *++argv;
208 if (strncmp(arg, "--", 2) == 0) {
209 options[@(arg + 2)] = @(*++argv); // --key value
210 } else {
211 [args addObject:@(arg)]; // positional argument
212 }
213 }
214
215As with all C pointers, character pointer expressions can involve
216arbitrary pointer arithmetic, therefore programmers must ensure that the
217character data is valid. Passing ``NULL`` as the character pointer will
218raise an exception at runtime. When possible, the compiler will reject
219``NULL`` character pointers used in boxed expressions.
220
221Availability
222------------
223
224Boxed expressions will be available in clang 3.2. It is not currently
225available in any Apple compiler.
226
227Container Literals
228==================
229
230Objective-C now supports a new expression syntax for creating immutable
231array and dictionary container objects.
232
233Examples
234--------
235
236Immutable array expression:
237
238.. code-block:: objc
239
240 NSArray *array = @[ @"Hello", NSApp, [NSNumber numberWithInt:42] ];
241
242This creates an ``NSArray`` with 3 elements. The comma-separated
243sub-expressions of an array literal can be any Objective-C object
244pointer typed expression.
245
246Immutable dictionary expression:
247
248.. code-block:: objc
249
250 NSDictionary *dictionary = @{
251 @"name" : NSUserName(),
252 @"date" : [NSDate date],
253 @"processInfo" : [NSProcessInfo processInfo]
254 };
255
256This creates an ``NSDictionary`` with 3 key/value pairs. Value
257sub-expressions of a dictionary literal must be Objective-C object
258pointer typed, as in array literals. Key sub-expressions must be of an
259Objective-C object pointer type that implements the
260``&LT;NSCopying&GT;`` protocol.
261
262Discussion
263----------
264
265Neither keys nor values can have the value ``nil`` in containers. If the
266compiler can prove that a key or value is ``nil`` at compile time, then
267a warning will be emitted. Otherwise, a runtime error will occur.
268
269Using array and dictionary literals is safer than the variadic creation
270forms commonly in use today. Array literal expressions expand to calls
271to ``+[NSArray arrayWithObjects:count:]``, which validates that all
272objects are non-``nil``. The variadic form,
273``+[NSArray arrayWithObjects:]`` uses ``nil`` as an argument list
274terminator, which can lead to malformed array objects. Dictionary
275literals are similarly created with
276``+[NSDictionary dictionaryWithObjects:forKeys:count:]`` which validates
277all objects and keys, unlike
278``+[NSDictionary dictionaryWithObjectsAndKeys:]`` which also uses a
279``nil`` parameter as an argument list terminator.
280
281Object Subscripting
282===================
283
284Objective-C object pointer values can now be used with C's subscripting
285operator.
286
287Examples
288--------
289
290The following code demonstrates the use of object subscripting syntax
291with ``NSMutableArray`` and ``NSMutableDictionary`` objects:
292
293.. code-block:: objc
294
295 NSMutableArray *array = ...;
296 NSUInteger idx = ...;
297 id newObject = ...;
298 id oldObject = array[idx];
299 array[idx] = newObject; // replace oldObject with newObject
300
301 NSMutableDictionary *dictionary = ...;
302 NSString *key = ...;
303 oldObject = dictionary[key];
304 dictionary[key] = newObject; // replace oldObject with newObject
305
306The next section explains how subscripting expressions map to accessor
307methods.
308
309Subscripting Methods
310--------------------
311
312Objective-C supports two kinds of subscript expressions: *array-style*
313subscript expressions use integer typed subscripts; *dictionary-style*
314subscript expressions use Objective-C object pointer typed subscripts.
315Each type of subscript expression is mapped to a message send using a
316predefined selector. The advantage of this design is flexibility: class
317designers are free to introduce subscripting by declaring methods or by
318adopting protocols. Moreover, because the method names are selected by
319the type of the subscript, an object can be subscripted using both array
320and dictionary styles.
321
322Array-Style Subscripting
323^^^^^^^^^^^^^^^^^^^^^^^^
324
325When the subscript operand has an integral type, the expression is
326rewritten to use one of two different selectors, depending on whether
327the element is being read or written. When an expression reads an
328element using an integral index, as in the following example:
329
330.. code-block:: objc
331
332 NSUInteger idx = ...;
333 id value = object[idx];
334
335it is translated into a call to ``objectAtIndexedSubscript:``
336
337.. code-block:: objc
338
339 id value = [object objectAtIndexedSubscript:idx];
340
341When an expression writes an element using an integral index:
342
343.. code-block:: objc
344
345 object[idx] = newValue;
346
347it is translated to a call to ``setObject:atIndexedSubscript:``
348
349.. code-block:: objc
350
351 [object setObject:newValue atIndexedSubscript:idx];
352
353These message sends are then type-checked and performed just like
354explicit message sends. The method used for objectAtIndexedSubscript:
355must be declared with an argument of integral type and a return value of
356some Objective-C object pointer type. The method used for
357setObject:atIndexedSubscript: must be declared with its first argument
358having some Objective-C pointer type and its second argument having
359integral type.
360
361The meaning of indexes is left up to the declaring class. The compiler
362will coerce the index to the appropriate argument type of the method it
363uses for type-checking. For an instance of ``NSArray``, reading an
364element using an index outside the range ``[0, array.count)`` will raise
365an exception. For an instance of ``NSMutableArray``, assigning to an
366element using an index within this range will replace that element, but
367assigning to an element using an index outside this range will raise an
368exception; no syntax is provided for inserting, appending, or removing
369elements for mutable arrays.
370
371A class need not declare both methods in order to take advantage of this
372language feature. For example, the class ``NSArray`` declares only
373``objectAtIndexedSubscript:``, so that assignments to elements will fail
374to type-check; moreover, its subclass ``NSMutableArray`` declares
375``setObject:atIndexedSubscript:``.
376
377Dictionary-Style Subscripting
378^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
379
380When the subscript operand has an Objective-C object pointer type, the
381expression is rewritten to use one of two different selectors, depending
382on whether the element is being read from or written to. When an
383expression reads an element using an Objective-C object pointer
384subscript operand, as in the following example:
385
386.. code-block:: objc
387
388 id key = ...;
389 id value = object[key];
390
391it is translated into a call to the ``objectForKeyedSubscript:`` method:
392
393.. code-block:: objc
394
395 id value = [object objectForKeyedSubscript:key];
396
397When an expression writes an element using an Objective-C object pointer
398subscript:
399
400.. code-block:: objc
401
402 object[key] = newValue;
403
404it is translated to a call to ``setObject:forKeyedSubscript:``
405
406.. code-block:: objc
407
408 [object setObject:newValue forKeyedSubscript:key];
409
410The behavior of ``setObject:forKeyedSubscript:`` is class-specific; but
411in general it should replace an existing value if one is already
412associated with a key, otherwise it should add a new value for the key.
413No syntax is provided for removing elements from mutable dictionaries.
414
415Discussion
416----------
417
418An Objective-C subscript expression occurs when the base operand of the
419C subscript operator has an Objective-C object pointer type. Since this
420potentially collides with pointer arithmetic on the value, these
421expressions are only supported under the modern Objective-C runtime,
422which categorically forbids such arithmetic.
423
424Currently, only subscripts of integral or Objective-C object pointer
425type are supported. In C++, a class type can be used if it has a single
426conversion function to an integral or Objective-C pointer type, in which
427case that conversion is applied and analysis continues as appropriate.
428Otherwise, the expression is ill-formed.
429
430An Objective-C object subscript expression is always an l-value. If the
431expression appears on the left-hand side of a simple assignment operator
432(=), the element is written as described below. If the expression
433appears on the left-hand side of a compound assignment operator (e.g.
434+=), the program is ill-formed, because the result of reading an element
435is always an Objective-C object pointer and no binary operators are
436legal on such pointers. If the expression appears in any other position,
437the element is read as described below. It is an error to take the
438address of a subscript expression, or (in C++) to bind a reference to
439it.
440
441Programs can use object subscripting with Objective-C object pointers of
442type ``id``. Normal dynamic message send rules apply; the compiler must
443see *some* declaration of the subscripting methods, and will pick the
444declaration seen first.
445
446Caveats
447=======
448
449Objects created using the literal or boxed expression syntax are not
450guaranteed to be uniqued by the runtime, but nor are they guaranteed to
451be newly-allocated. As such, the result of performing direct comparisons
452against the location of an object literal (using ``==``, ``!=``, ``<``,
453``<=``, ``>``, or ``>=``) is not well-defined. This is usually a simple
454mistake in code that intended to call the ``isEqual:`` method (or the
455``compare:`` method).
456
457This caveat applies to compile-time string literals as well.
458Historically, string literals (using the ``@"..."`` syntax) have been
459uniqued across translation units during linking. This is an
460implementation detail of the compiler and should not be relied upon. If
461you are using such code, please use global string constants instead
462(``NSString * const MyConst = @"..."``) or use ``isEqual:``.
463
464Grammar Additions
465=================
466
467To support the new syntax described above, the Objective-C
468``@``-expression grammar has the following new productions:
469
470::
471
472 objc-at-expression : '@' (string-literal | encode-literal | selector-literal | protocol-literal | object-literal)
473 ;
474
475 object-literal : ('+' | '-')? numeric-constant
476 | character-constant
477 | boolean-constant
478 | array-literal
479 | dictionary-literal
480 ;
481
482 boolean-constant : '__objc_yes' | '__objc_no' | 'true' | 'false' /* boolean keywords. */
483 ;
484
485 array-literal : '[' assignment-expression-list ']'
486 ;
487
488 assignment-expression-list : assignment-expression (',' assignment-expression-list)?
489 | /* empty */
490 ;
491
492 dictionary-literal : '{' key-value-list '}'
493 ;
494
495 key-value-list : key-value-pair (',' key-value-list)?
496 | /* empty */
497 ;
498
499 key-value-pair : assignment-expression ':' assignment-expression
500 ;
501
502Note: ``@true`` and ``@false`` are only supported in Objective-C++.
503
504Availability Checks
505===================
506
507Programs test for the new features by using clang's \_\_has\_feature
508checks. Here are examples of their use:
509
510.. code-block:: objc
511
512 #if __has_feature(objc_array_literals)
513 // new way.
514 NSArray *elements = @[ @"H", @"He", @"O", @"C" ];
515 #else
516 // old way (equivalent).
517 id objects[] = { @"H", @"He", @"O", @"C" };
518 NSArray *elements = [NSArray arrayWithObjects:objects count:4];
519 #endif
520
521 #if __has_feature(objc_dictionary_literals)
522 // new way.
523 NSDictionary *masses = @{ @"H" : @1.0078, @"He" : @4.0026, @"O" : @15.9990, @"C" : @12.0096 };
524 #else
525 // old way (equivalent).
526 id keys[] = { @"H", @"He", @"O", @"C" };
527 id values[] = { [NSNumber numberWithDouble:1.0078], [NSNumber numberWithDouble:4.0026],
528 [NSNumber numberWithDouble:15.9990], [NSNumber numberWithDouble:12.0096] };
529 NSDictionary *masses = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:4];
530 #endif
531
532 #if __has_feature(objc_subscripting)
533 NSUInteger i, count = elements.count;
534 for (i = 0; i < count; ++i) {
535 NSString *element = elements[i];
536 NSNumber *mass = masses[element];
537 NSLog(@"the mass of %@ is %@", element, mass);
538 }
539 #else
540 NSUInteger i, count = [elements count];
541 for (i = 0; i < count; ++i) {
542 NSString *element = [elements objectAtIndex:i];
543 NSNumber *mass = [masses objectForKey:element];
544 NSLog(@"the mass of %@ is %@", element, mass);
545 }
546 #endif
547
548Code can use also ``__has_feature(objc_bool)`` to check for the
549availability of numeric literals support. This checks for the new
550``__objc_yes / __objc_no`` keywords, which enable the use of
551``@YES / @NO`` literals.
552
553To check whether boxed expressions are supported, use
554``__has_feature(objc_boxed_expressions)`` feature macro.