You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Clang provides support for <ahref="ObjectiveCLiterals.html">Object Literals and Subscripting</a> in Objective-C, which simplifies common Objective-C programming patterns, makes programs more concise, and improves the safety of container creation. There are several feature macros associated with object literals and subscripting: <code>__has_feature(objc_array_literals)</code> tests the availability of array literals; <code>__has_feature(objc_dictionary_literals)</code> tests the availability of dictionary literals; <code>__has_feature(objc_subscripting)</code> tests the availability of object subscripting.</p>
1189
+
<p>Clang provides support for <ahref="ObjectiveCLiterals.html">Object Literals
1190
+
and Subscripting</a> in Objective-C, which simplifies common Objective-C
1191
+
programming patterns, makes programs more concise, and improves the safety of
1192
+
container creation. There are several feature macros associated with object
1193
+
literals and subscripting: <code>__has_feature(objc_array_literals)</code>
1194
+
tests the availability of array literals;
1195
+
<code>__has_feature(objc_dictionary_literals)</code> tests the availability of
1196
+
dictionary literals; <code>__has_feature(objc_subscripting)</code> tests the
Copy file name to clipboardExpand all lines: clang/docs/ObjectiveCLiterals.html
+97-3Lines changed: 97 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -68,14 +68,15 @@ <h3>Examples</h3>
68
68
69
69
<h3>Discussion</h3>
70
70
71
-
NSNumber literals only support literal scalar values after the '@'. Consequently, @INT_MAX works, but @INT_MIN does not, because they are defined like this:<p>
71
+
NSNumber literals only support literal scalar values after the <code>'@'</code>. Consequently, <code>@INT_MAX</code> works, but <code>@INT_MIN</code> does not, because they are defined like this:<p>
72
72
73
73
<pre>
74
74
#define INT_MAX 2147483647 /* max value for an int */
75
75
#define INT_MIN (-2147483647-1) /* min value for an int */
76
76
</pre>
77
77
78
-
The definition of INT_MIN is not a simple literal, but a parenthesized expression. This is by design, but may be improved in subsequent compiler releases.<p>
78
+
The definition of <code>INT_MIN</code> is not a simple literal, but a parenthesized expression. Parenthesized
79
+
expressions are supported using the <ahref="#objc_boxed_expressions">boxed expression</a> syntax, which is described in the next section.<p>
79
80
80
81
Because <code>NSNumber</code> does not currently support wrapping <code>long double</code> values, the use of a <code>long double NSNumber</code> literal (e.g. <code>@123.23L</code>) will be rejected by the compiler.<p>
81
82
@@ -95,6 +96,94 @@ <h3>Discussion</h3>
95
96
96
97
Objective-C++ also supports <code>@true</code> and <code>@false</code> expressions, which are equivalent to <code>@YES</code> and <code>@NO</code>.
Cocoa frameworks frequently define constant values using <em>enums.</em> Although enum values are integral, they may not be used directly as boxed literals (this avoids conflicts with future <code>'@'</code>-prefixed Objective-C keywords). Instead, an enum value must be placed inside a boxed expression. The following example demonstrates configuring an <code>AVAudioRecorder</code> using a dictionary that contains a boxed enumeration value:
The expression <code>@(AVAudioQualityMax)</code> converts <code>AVAudioQualityMax</code> to an integer type, and boxes the value accordingly. If the enum has a <ahref="http://clang.llvm.org/docs/LanguageExtensions.html#objc_fixed_enum">fixed underlying type</a> as in:
then the fixed underlying type will be used to select the correct <code>NSNumber</code> creation method.
158
+
</p>
159
+
160
+
<h3>Boxed C Strings</h3>
161
+
162
+
<p>
163
+
A C string literal prefixed by the <code>'@'</code> token denotes an <code>NSString</code> literal in the same way a numeric literal prefixed by the <code>'@'</code> token denotes an <code>NSNumber</code> literal. When the type of the parenthesized expression is <code>(char *)</code> or <code>(const char *)</code>, the result of the boxed expression is a pointer to an <code>NSString</code> object containing equivalent character data. The following example converts C-style command line arguments into <code>NSString</code> objects.
164
+
</p>
165
+
166
+
<pre>
167
+
// Partition command line arguments into positional and option arguments.
As with all C pointers, character pointer expressions can involve arbitrary pointer arithmetic, therefore programmers must ensure that the character data is valid. Passing <code>NULL</code> as the character pointer will raise an exception at runtime. When possible, the compiler will reject <code>NULL</code> character pointers used in boxed expressions.
182
+
</p>
183
+
184
+
<h3>Availability</h3>
185
+
186
+
<p>This feature will be available in clang 3.2. It is not currently available in any Apple compiler.</p>
This creates an <code>NSArray</code> with 3 elements. The comma-separated sub-expressions of an array literal can be any Objective-C object pointer typed expression.<p>
112
203
@@ -309,6 +400,9 @@ <h2>Availability Checks</h2>
309
400
310
401
Code can use also <code>__has_feature(objc_bool)</code> to check for the availability of numeric literals support. This checks for the new <code>__objc_yes / __objc_no</code> keywords, which enable the use of <code>@YES / @NO</code> literals.<p>
311
402
403
+
<p>To check whether boxed expressions are supported, use
0 commit comments