在Objective-C中是否有一些字面字典或数组语法? [英] Is there some literal dictionary or array syntax in Objective-C?

查看:130
本文介绍了在Objective-C中是否有一些字面字典或数组语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

始终可以使用vararg方法调用创建NSArrays(和NSDictionaries / NSNumber),例如:

It's always been possible to create NSArrays (and NSDictionaries/NSNumber) with vararg method calls, like:

[NSArray arrayWithObjects: @"a", @"b", @"c", nil];

是否可以使用内联文字创建LLVM和Clang的新改进?

Can these be created with in-line literals in a new improvement to LLVM and Clang?

推荐答案

使用对LLVM代码库的此更改,Apple在即将到来的Clang编译器版本中为文字添加了新的语法。

With this change to the LLVM codebase, Apple has added a new syntax for literals in upcoming versions of the Clang compiler.

之前,数组是使用基于C的数组创建的,并在运行时转换为Objective-C对象,例如:

Before, arrays were created using a C-based array and were converted on the fly into Objective-C objects, such as:

NSArray* array = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];

注意,因为这是一个varargs元素,所以你必须在结尾提供一个结尾nil的列表。但是,现在有一个更简单的方法:

Note that since this is a varargs element, you had to supply an ending 'nil' at the end of the list. However, now there's an easier way:

NSArray* array = @[ @"One", @"Two", @"Three" ];

请注意,在[]之前的前导@是必需的,以区分它和一个和普通C数组(或消息发送)。请注意,不再需要尾随的'nil'。

Note that the leading @ before the [] is required, to distinguish between it and an and ordinary C array (or a message send). Note also that the trailing 'nil' is no longer required.

类似于JSON结构的类似的内联字典文字变化:

A similar change has been made for in-line dictionary literals, similar to JSON structures:

NSDictionary* dict = @{
    @"Key1": @"Value1",
    @"Key2": @"Value2",
};

最后,添加了一个新的NSInteger(等)文字:

Finally, a new literal for NSInteger (etc.) has been added:

NSNumber* value = @3.141;

请注意,虽然这适用于浮点( @ 3.141F )和双精度( @ 3.141 )不适用于 long double 由编译器包装。因此, @ 3.141D 将是编译时错误。

Note that although this works for floating point (@3.141F) and doubles (@3.141) it does not work for long doubles as these are not supported for wrapping by the compiler. Thus, @3.141D will be a compile-time error.

由于定义常量, code> @INT_MAX 是有效的有效值,但 @INT_MIN 不是,因为后者是通过编译时表达式定义的,

Owing to how the constants are defined, @INT_MAX is a valid valid value but @INT_MIN is not, since the latter is defined via a compile-time expression and not a literal in itself.

还有对布尔类型的扩展:

There are also extensions to boolean types:

NSNumber* yes = @YES;         // [NSNumber numberWithBool:YES]
NSNumber* no = @NO;           // [NSNumber numberWithBool:NO]
NSNumber* trueBool = @true;   // [NSNumber numberWithBool:(BOOL)true]
NSNumber* falseBool = @false; // [NSNumber numberWithBool:(BOOL)false]

此更改还引入了 __ objc_yes __objc_no 字面值以支持通过文字值解析类型。它们的使用在预处理器中用 #if __has_feature(objc_bool)保护,但开发人员应继续使用 YES code> NO 。

This change has also introduced the __objc_yes and __objc_no literals to support the parsing of the types via literal value only. Their use is guarded with #if __has_feature(objc_bool) in the preprocessor, but developers should continue to use YES and NO in code.

最后,数组和字典现在可以用数组括号来下标, code> lvalue 和 rvalue 表达式:

Finally, both arrays and dictionaries can now be subscripted with array brackets, in use both as lvalue and rvalue expressions:

NSMutableArray* stuff = ...
id first = stuff[0];
stuff[0] = anotherObject;

NSMutableDictionary* moreStuff = ...
id conference = moreStuff[@"NSConf"]
moreStuff[@"SponsoredBy"] = @"NSConfDuck"

数组样式下标(使用 NSUInteger )映射到 objectAtIndexedSubscript:和相应的 setObject:atIndexedSubscript:,而字典访问使用 objectForKeyedSubscript: setObject:forKeyedSubscript:

The array style subscripting (using an NSUInteger) is mapped to objectAtIndexedSubscript: and the corresponding setObject:atIndexedSubscript:, whilst the dictionary access is accessed with objectForKeyedSubscript: and setObject:forKeyedSubscript:

文字的完整语法 Clang / LLVM网站

注意,由于这个答案最初是写的,Clang添加了对非文字Objective-C表达式Boxed表达式的支持

Note that since this answer was initially written, Clang has added support for non-literal Objective-C expressions called 'Boxed expressions'

这意味着可以使用<$ @ 7 @(Hello World)的等价类型c $ c> @(3 + 4) )为 @Hello World。注意,计算结果为 null 的C表达式将导致异常,并且诸如 @(null)的参数

This means that one can use @(3+4) as an equivalent to @7, and @("Hello World") as @"Hello World". Note that a C expression which evaluates to null will result in an exception, and arguments such as @(null) are treated as a compile-time error.

对于已知类型的类型,也可以使用Boxed枚举,因此

It is also possible to use 'Boxed enums' for types with a known type, so

枚举{
North,
South,
East,
West,
};

enum { North, South, East, West, };

可以放入带有 @(North)的盒装枚举类型,其值为 0

can be placed into a boxed enum type with @(North), which will have the value 0.

包含的表达式将在clang 3.2中提供。它可以使用 __ has_feature(objc_boxed_expressions)预处理器测试进行测试。

Boxed expressions will be available in clang 3.2 onwards. It can be tested for using the __has_feature(objc_boxed_expressions) preprocessor test.

这篇关于在Objective-C中是否有一些字面字典或数组语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆