# 1.  The literal syntax also works for expressions:

int x = 5;
float y = 6.32f;
NSNumber *expressionNumber = @(x * y);

# 2. However, you need to be aware of one thing when creating arrays using the literal syntax. If any of the objects is nil, an exception is thrown

NSArray *arrayA = [NSArray arrayWithObjects:
object1, object2, object3, nil];
NSArray *arrayB = @[object1, object2, object3];

Now consider the scenario in which object1 and object3 point to valid Objective-C objects, but object2 is nil. The literal array, arrayB, will cause the exception to be thrownHowever, arrayA will still be created but will contain only object1. The reason is that thearrayWithObjects: method looks through the variadic arguments until it hits nil, which is sooner than expected.

This subtle difference means that literals are much safer. It’s much better that an exception is thrown, causing a probable application crash, rather than creating an array having fewer than the expected number of objects in it. A programmer error most likely caused nil to be inserted into the array, and the exception means that the bug can be found more easily.

# 3. Also, in the case of strings, arrays, and dictionaries, only immutable variants can be created with the literal syntax. If a mutable variant is required, a mutable copy must be taken, like so:

NSMutableArray *mutable = [@[@1, @2, @3, @4, @5] mutableCopy];

• 本文已收录于以下专栏：

## Effective Objective-C 2.0: Item 41: Prefer Dispatch Queues to Locks for Synchronization

Item 41: Prefer Dispatch Queues to Locks for Synchronization Sometimes in Objective-C, you will com...

## Effective Objective-C 2.0：Item 48: Prefer Block Enumeration to for Loops

Item 48: Prefer Block Enumeration to for Loops Enumerating a collection is a very common task in ...

## Effective Objective-C 2.0： Item 4: Prefer Typed Constants to Preprocessor #define

Item 4: Prefer Typed Constants to Preprocessor #define 曾经面试被被问到 Preprocessor #define 有什么缺点， 这篇文章很...

## Effective Objective-C 2.0: Item 43: Know When to Use GCD and When to Use Operation Queues

Item 43: Know When to Use GCD and When to Use Operation Queues GCD is a fantastic technology, but...

## Effective Objective-C 2.0: Item 16:Designated Initializer

Item 16: Have a Designated Initializer All objects need to be initialized. When initializing an o...

## Effective Objective-C 2.0：Item 25: Always Prefix Category Names on Third-Party Classes

Item 25: Always Prefix Category Names on Third-Party Classes Categories are commonly used to add ...

## Effective Objective-C 2.0：Item 14 Class Object

Item 14: Understand What a Class Object Is Objective-C is extremely dynamic in nature. Item 11 expl...

## Effective Objective-C 2.0：Item 20: Prefix Private Method Names

Item 20: Prefix Private Method Names It’s extremely common for a class to do much more than it ap...

## Effective Objective-C 2.0: Item 35: Use Zombies to Help Debug Memory-Management Problems

Item 35: Use Zombies to Help Debug Memory-Management Problems Debugging memory-management issues ...

## Effective Objective-C 2.0： Item 12: Understand Message Forwarding

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)