diff --git a/clang/docs/ObjectiveCLiterals.html b/clang/docs/ObjectiveCLiterals.html index f7f7348d6e7e..000c713697f4 100644 --- a/clang/docs/ObjectiveCLiterals.html +++ b/clang/docs/ObjectiveCLiterals.html @@ -24,21 +24,21 @@
NSNumber
from scalar literal expressions; Collection Literals provide a short-hand for creating arrays and dictionaries; Object Subscripting provides a way to use subscripting with Objective-C objects. Users of Apple compiler releases can use these features starting with the Apple LLVM Compiler 4.0. Users of open-source LLVM.org compiler releases can use these features starting with clang v3.1.+
Three new features were introduced into clang at the same time: NSNumber Literals provide a syntax for creating NSNumber
from scalar literal expressions; Collection Literals provide a short-hand for creating arrays and dictionaries; Object Subscripting provides a way to use subscripting with Objective-C objects. Users of Apple compiler releases can use these features starting with the Apple LLVM Compiler 4.0. Users of open-source LLVM.org compiler releases can use these features starting with clang v3.1.
+
These language additions simplify common Objective-C programming patterns, make programs more concise, and improve the safety of container creation.
-This document describes how the features are implemented in clang, and how to use them in your own programs.+
This document describes how the features are implemented in clang, and how to use them in your own programs.
NSNumber
is used to wrap scalar values inside objects: signed and unsigned integers (char
, short
, int
, long
, long long
), floating point numbers (float
, double
), and boolean values (BOOL
, C++ bool
). Scalar values wrapped in objects are also known as boxed values.+
The framework class NSNumber
is used to wrap scalar values inside objects: signed and unsigned integers (char
, short
, int
, long
, long long
), floating point numbers (float
, double
), and boolean values (BOOL
, C++ bool
). Scalar values wrapped in objects are also known as boxed values.
'@'
character will evaluate to a pointer to an NSNumber
object initialized with that value. C's type suffixes may be used to control the size of numeric literals.
+In Objective-C, any character, numeric or boolean literal prefixed with the '@'
character will evaluate to a pointer to an NSNumber
object initialized with that value. C's type suffixes may be used to control the size of numeric literals.
NSNumber
literals:+
The following program illustrates the rules for NSNumber
literals:
void main(int argc, const char *argv[]) { @@ -68,19 +68,19 @@ void main(int argc, const char *argv[]) {Discussion
-NSNumber literals only support literal scalar values after the'@'
. Consequently,@INT_MAX
works, but@INT_MIN
does not, because they are defined like this:+
NSNumber literals only support literal scalar values after the
'@'
. Consequently,@INT_MAX
works, but@INT_MIN
does not, because they are defined like this:#define INT_MAX 2147483647 /* max value for an int */ #define INT_MIN (-2147483647-1) /* min value for an int */-The definition ofINT_MIN
is not a simple literal, but a parenthesized expression. Parenthesized -expressions are supported using the boxed expression syntax, which is described in the next section.+
The definition of
-BecauseINT_MIN
is not a simple literal, but a parenthesized expression. Parenthesized +expressions are supported using the boxed expression syntax, which is described in the next section.NSNumber
does not currently support wrappinglong double
values, the use of along double NSNumber
literal (e.g.@123.23L
) will be rejected by the compiler.+
Because
-Previously, theNSNumber
does not currently support wrappinglong double
values, the use of along double NSNumber
literal (e.g.@123.23L
) will be rejected by the compiler.BOOL
type was simply a typedef forsigned char
, andYES
andNO
were macros that expand to(BOOL)1
and(BOOL)0
respectively. To support@YES
and@NO
expressions, these macros are now defined using new language keywords in<objc/objc.h>
:+
Previously, the
BOOL
type was simply a typedef forsigned char
, andYES
andNO
were macros that expand to(BOOL)1
and(BOOL)0
respectively. To support@YES
and@NO
expressions, these macros are now defined using new language keywords in<objc/objc.h>
:#if __has_feature(objc_bool) @@ -92,9 +92,9 @@ Previously, the-The compiler implicitly convertsBOOL
type was simply a typedef forsigned cha #endif
__objc_yes
and__objc_no
to(BOOL)1
and(BOOL)0
. The keywords are used to disambiguateBOOL
and integer literals.+
The compiler implicitly converts
-Objective-C++ also supports__objc_yes
and__objc_no
to(BOOL)1
and(BOOL)0
. The keywords are used to disambiguateBOOL
and integer literals.@true
and@false
expressions, which are equivalent to@YES
and@NO
. +Objective-C++ also supports
@true
and@false
expressions, which are equivalent to@YES
and@NO
.Boxed Expressions
@@ -160,7 +160,7 @@ then the fixed underlying type will be used to select the correctNSNumber
Boxed C Strings
-A C string literal prefixed by the
'@'
token denotes anNSString
literal in the same way a numeric literal prefixed by the'@'
token denotes anNSNumber
literal. When the type of the parenthesized expression is(char *)
or(const char *)
, the result of the boxed expression is a pointer to anNSString
object containing equivalent character data. The following example converts C-style command line arguments intoNSString
objects. +A C string literal prefixed by the'@'
token denotes anNSString
literal in the same way a numeric literal prefixed by the'@'
token denotes anNSNumber
literal. When the type of the parenthesized expression is(char *)
or(const char *)
, the result of the boxed expression is a pointer to anNSString
object containing equivalent character data, which is assumed to be '\0'-terminated and UTF-8 encoded. The following example converts C-style command line arguments intoNSString
objects.@@ -183,25 +183,23 @@ As with all C pointers, character pointer expressions can involve arbitrary poinAvailability
-This feature will be available after clang 3.1. It is not currently available in any Apple compiler.
+Boxed expressions will be available in clang 3.2. It is not currently available in any Apple compiler.
Container Literals
-Objective-C now supports a new expression syntax for creating immutable array and dictionary container objects. +Objective-C now supports a new expression syntax for creating immutable array and dictionary container objects.
Examples
-Immutable array expression:+
Immutable array expression:
--This creates anNSArray *array = @[ @"Hello", NSApp, [NSNumber numberWithInt:42] ];-NSArray
with 3 elements. The comma-separated sub-expressions of an array literal can be any Objective-C object pointer typed expression.+
This creates an
-Immutable dictionary expression:NSArray
with 3 elements. The comma-separated sub-expressions of an array literal can be any Objective-C object pointer typed expression.+
Immutable dictionary expression:
NSDictionary *dictionary = @{ @@ -211,21 +209,21 @@ NSDictionary *dictionary = @{ };-This creates anNSDictionary
with 3 key/value pairs. Value sub-expressions of a dictionary literal must be Objective-C object pointer typed, as in array literals. Key sub-expressions must be of an Objective-C object pointer type that implements the<NSCopying>
protocol.+
This creates an
NSDictionary
with 3 key/value pairs. Value sub-expressions of a dictionary literal must be Objective-C object pointer typed, as in array literals. Key sub-expressions must be of an Objective-C object pointer type that implements the<NSCopying>
protocol.Discussion
-Neither keys nor values can have the valuenil
in containers. If the compiler can prove that a key or value isnil
at compile time, then a warning will be emitted. Otherwise, a runtime error will occur.+
Neither keys nor values can have the value
-Using array and dictionary literals is safer than the variadic creation forms commonly in use today. Array literal expressions expand to calls tonil
in containers. If the compiler can prove that a key or value isnil
at compile time, then a warning will be emitted. Otherwise, a runtime error will occur.+[NSArray arrayWithObjects:count:]
, which validates that all objects are non-nil
. The variadic form,+[NSArray arrayWithObjects:]
usesnil
as an argument list terminator, which can lead to malformed array objects. Dictionary literals are similarly created with+[NSDictionary dictionaryWithObjects:forKeys:count:]
which validates all objects and keys, unlike+[NSDictionary dictionaryWithObjectsAndKeys:]
which also uses anil
parameter as an argument list terminator.+
Using array and dictionary literals is safer than the variadic creation forms commonly in use today. Array literal expressions expand to calls to
+[NSArray arrayWithObjects:count:]
, which validates that all objects are non-nil
. The variadic form,+[NSArray arrayWithObjects:]
usesnil
as an argument list terminator, which can lead to malformed array objects. Dictionary literals are similarly created with+[NSDictionary dictionaryWithObjects:forKeys:count:]
which validates all objects and keys, unlike+[NSDictionary dictionaryWithObjectsAndKeys:]
which also uses anil
parameter as an argument list terminator.Object Subscripting
-Objective-C object pointer values can now be used with C's subscripting operator.+
Objective-C object pointer values can now be used with C's subscripting operator.
Examples
-The following code demonstrates the use of object subscripting syntax withNSMutableArray
andNSMutableDictionary
objects:+
The following code demonstrates the use of object subscripting syntax with
NSMutableArray
andNSMutableDictionary
objects:NSMutableArray *array = ...; @@ -240,87 +238,87 @@ oldObject = dictionary[key]; dictionary[key] = newObject; // replace oldObject with newObject-The next section explains how subscripting expressions map to accessor methods.+
The next section explains how subscripting expressions map to accessor methods.
Subscripting Methods
-Objective-C supports two kinds of subscript expressions: array-style subscript expressions use integer typed subscripts; dictionary-style subscript expressions use Objective-C object pointer typed subscripts. Each type of subscript expression is mapped to a message send using a predefined selector. The advantage of this design is flexibility: class designers are free to introduce subscripting by declaring methods or by adopting protocols. Moreover, because the method names are selected by the type of the subscript, an object can be subscripted using both array and dictionary styles. +Objective-C supports two kinds of subscript expressions: array-style subscript expressions use integer typed subscripts; dictionary-style subscript expressions use Objective-C object pointer typed subscripts. Each type of subscript expression is mapped to a message send using a predefined selector. The advantage of this design is flexibility: class designers are free to introduce subscripting by declaring methods or by adopting protocols. Moreover, because the method names are selected by the type of the subscript, an object can be subscripted using both array and dictionary styles.
Array-Style Subscripting
-When the subscript operand has an integral type, the expression is rewritten to use one of two different selectors, depending on whether the element is being read or written. When an expression reads an element using an integral index, as in the following example:+
When the subscript operand has an integral type, the expression is rewritten to use one of two different selectors, depending on whether the element is being read or written. When an expression reads an element using an integral index, as in the following example:
NSUInteger idx = ...; id value = object[idx];-it is translated into a call toobjectAtIndexedSubscript:
+
it is translated into a call to
objectAtIndexedSubscript:
id value = [object objectAtIndexedSubscript:idx];-When an expression writes an element using an integral index:+
When an expression writes an element using an integral index:
object[idx] = newValue;-it is translated to a call tosetObject:atIndexedSubscript:
+
it is translated to a call to
setObject:atIndexedSubscript:
[object setObject:newValue atIndexedSubscript:idx];-These message sends are then type-checked and performed just like explicit message sends. The method used for objectAtIndexedSubscript: must be declared with an argument of integral type and a return value of some Objective-C object pointer type. The method used for setObject:atIndexedSubscript: must be declared with its first argument having some Objective-C pointer type and its second argument having integral type.+
These message sends are then type-checked and performed just like explicit message sends. The method used for objectAtIndexedSubscript: must be declared with an argument of integral type and a return value of some Objective-C object pointer type. The method used for setObject:atIndexedSubscript: must be declared with its first argument having some Objective-C pointer type and its second argument having integral type.
-The meaning of indexes is left up to the declaring class. The compiler will coerce the index to the appropriate argument type of the method it uses for type-checking. For an instance ofNSArray
, reading an element using an index outside the range[0, array.count)
will raise an exception. For an instance ofNSMutableArray
, assigning to an element using an index within this range will replace that element, but assigning to an element using an index outside this range will raise an exception; no syntax is provided for inserting, appending, or removing elements for mutable arrays.+
The meaning of indexes is left up to the declaring class. The compiler will coerce the index to the appropriate argument type of the method it uses for type-checking. For an instance of
-A class need not declare both methods in order to take advantage of this language feature. For example, the classNSArray
, reading an element using an index outside the range[0, array.count)
will raise an exception. For an instance ofNSMutableArray
, assigning to an element using an index within this range will replace that element, but assigning to an element using an index outside this range will raise an exception; no syntax is provided for inserting, appending, or removing elements for mutable arrays.NSArray
declares onlyobjectAtIndexedSubscript:
, so that assignments to elements will fail to type-check; moreover, its subclassNSMutableArray
declaressetObject:atIndexedSubscript:
. +A class need not declare both methods in order to take advantage of this language feature. For example, the class
NSArray
declares onlyobjectAtIndexedSubscript:
, so that assignments to elements will fail to type-check; moreover, its subclassNSMutableArray
declaressetObject:atIndexedSubscript:
.Dictionary-Style Subscripting
-When the subscript operand has an Objective-C object pointer type, the expression is rewritten to use one of two different selectors, depending on whether the element is being read from or written to. When an expression reads an element using an Objective-C object pointer subscript operand, as in the following example:+
When the subscript operand has an Objective-C object pointer type, the expression is rewritten to use one of two different selectors, depending on whether the element is being read from or written to. When an expression reads an element using an Objective-C object pointer subscript operand, as in the following example:
id key = ...; id value = object[key];-it is translated into a call to theobjectForKeyedSubscript:
method:+
it is translated into a call to the
objectForKeyedSubscript:
method:id value = [object objectForKeyedSubscript:key];-When an expression writes an element using an Objective-C object pointer subscript:+
When an expression writes an element using an Objective-C object pointer subscript:
object[key] = newValue;-it is translated to a call tosetObject:forKeyedSubscript:
+it is translated to a call to
setObject:forKeyedSubscript:
[object setObject:newValue forKeyedSubscript:key];-The behavior ofsetObject:forKeyedSubscript:
is class-specific; but in general it should replace an existing value if one is already associated with a key, otherwise it should add a new value for the key. No syntax is provided for removing elements from mutable dictionaries.+
The behavior of
setObject:forKeyedSubscript:
is class-specific; but in general it should replace an existing value if one is already associated with a key, otherwise it should add a new value for the key. No syntax is provided for removing elements from mutable dictionaries.Discussion
-An Objective-C subscript expression occurs when the base operand of the C subscript operator has an Objective-C object pointer type. Since this potentially collides with pointer arithmetic on the value, these expressions are only supported under the modern Objective-C runtime, which categorically forbids such arithmetic.+
An Objective-C subscript expression occurs when the base operand of the C subscript operator has an Objective-C object pointer type. Since this potentially collides with pointer arithmetic on the value, these expressions are only supported under the modern Objective-C runtime, which categorically forbids such arithmetic.
-Currently, only subscripts of integral or Objective-C object pointer type are supported. In C++, a class type can be used if it has a single conversion function to an integral or Objective-C pointer type, in which case that conversion is applied and analysis continues as appropriate. Otherwise, the expression is ill-formed.+
Currently, only subscripts of integral or Objective-C object pointer type are supported. In C++, a class type can be used if it has a single conversion function to an integral or Objective-C pointer type, in which case that conversion is applied and analysis continues as appropriate. Otherwise, the expression is ill-formed.
-An Objective-C object subscript expression is always an l-value. If the expression appears on the left-hand side of a simple assignment operator (=), the element is written as described below. If the expression appears on the left-hand side of a compound assignment operator (e.g. +=), the program is ill-formed, because the result of reading an element is always an Objective-C object pointer and no binary operators are legal on such pointers. If the expression appears in any other position, the element is read as described below. It is an error to take the address of a subscript expression, or (in C++) to bind a reference to it.+
An Objective-C object subscript expression is always an l-value. If the expression appears on the left-hand side of a simple assignment operator (=), the element is written as described below. If the expression appears on the left-hand side of a compound assignment operator (e.g. +=), the program is ill-formed, because the result of reading an element is always an Objective-C object pointer and no binary operators are legal on such pointers. If the expression appears in any other position, the element is read as described below. It is an error to take the address of a subscript expression, or (in C++) to bind a reference to it.
-Programs can use object subscripting with Objective-C object pointers of typeid
. Normal dynamic message send rules apply; the compiler must see some declaration of the subscripting methods, and will pick the declaration seen first.+
Programs can use object subscripting with Objective-C object pointers of type
id
. Normal dynamic message send rules apply; the compiler must see some declaration of the subscripting methods, and will pick the declaration seen first.Grammar Additions
-To support the new syntax described above, the Objective-C@
-expression grammar has the following new productions:+
To support the new syntax described above, the Objective-C
@
-expression grammar has the following new productions:objc-at-expression : '@' (string-literal | encode-literal | selector-literal | protocol-literal | object-literal) @@ -354,11 +352,11 @@ key-value-pair : assignment-expression ':' assignment-expression ;-Note:@true
and@false
are only supported in Objective-C++.+
Note:
@true
and@false
are only supported in Objective-C++.Availability Checks
-Programs test for the new features by using clang's __has_feature checks. Here are examples of their use:+
Programs test for the new features by using clang's __has_feature checks. Here are examples of their use:
#if __has_feature(objc_array_literals) @@ -398,10 +396,9 @@ Programs test for the new features by using clang's __has_feature checks. Here a #endif-Code can use also__has_feature(objc_bool)
to check for the availability of numeric literals support. This checks for the new__objc_yes / __objc_no
keywords, which enable the use of@YES / @NO
literals.+
Code can use also
-__has_feature(objc_bool)
to check for the availability of numeric literals support. This checks for the new__objc_yes / __objc_no
keywords, which enable the use of@YES / @NO
literals.To check whether boxed expressions are supported, use -
+__has_feature(objc_boxed_expressions)
feature macro.To check whether boxed expressions are supported, use
__has_feature(objc_boxed_expressions)
feature macro.