Brief Intro to NSNumber Literals of Foundation Frameworks

NSNumber Literals

You may recall that Chapter 10 provided a brief overview of NSNumber literals. To recap, the Foundation Framework class NSNumber is a container for primitive (i.e., scalar) types. An instance ofNSNumber provides an object wrapper of a scalar value (integer value, floating-point value, or Boolean value). NSNumber objects are useful in a variety of scenarios; for example, with the Foundation Framework collection classes that can’t hold primitive types. However, the creation and initialization of an NSNumber requires the use of the NSNumber APIs, which can be tedious for cases with a lot of objects (e.g., a collection class initialized with many NSNumber objects).

An NSNumber literal uses a simple notation for creating and initializing an NSNumber object from a scalar literal expression, its syntax is

@ScalarValue

The literal begins with the ampersand (@) symbol, followed by a ScalarValue that represents any character, numeric, or Boolean literal value. Listing 16-2 demonstrates the use of NSNumber literals in a statement that creates an NSArray object named numbers.

Listing 16-2.  Creating an NSArray Instance Using NSNumber Literals

NSArray *numbers = [NSArray arrayWithObjects:@'A', @YES, @-3, @21U, @250L,
                    @9876543210LL, @3.14F, @-52.687, nil];

The use of NSNumber literals in the preceding statement greatly simplifies the amount of code you have to write, thereby reducing the chance for errors. So that’s all well and good, but you may be wondering, how does this all work? Well, the compiler translates each NSNumber literal into a convenience constructor that creates the corresponding NSNumber instance at runtime. In effect, the following two expressions are equivalent:

@17
[NSNumber numberWithInt:17]

The same is true for scalar data types with modifiers; the next two expressions are also equivalent:

@3.14f
[NSNumber numberWithFloat:3.14]

So when you write the NSNumber literal @17 in code, the compiler translates that into the expression[NSNumber numberWithInt:17]. In effect, the literal notation is a shortcut for an NSNumber object creation.

As NSNumber literals are, in effect, NSNumber objects, they have available the full range of NSNumberAPIs; for example, the following statement compares the values of two NSNumber literals.

NSComparisonResult comparison = [@17 compare:@16];

Note that NSNumber literal objects are created from scalar values, not expressions. Also be aware that NSNumber literals are evaluated at runtimeas such, they are not compile-time constants and hence cannot be used to initialize static or global variables. The following statement will not compile:

static NSNumber *badNumber = @0;              // ERROR!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值