Effective Objective-C 2.0: Item 3: Prefer Literal Syntax over the Equivalent Methods

原创 2013年12月02日 11:03:52

Item 3: Prefer Literal Syntax over the Equivalent Methods


第二点需要注意,元素值出现nil会throw Exception,相比,更安全(容易发现问题)。

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 12: Understand Message Forwarding

这点很有意思,理清楚了Message Forward的过程(特别那个例子有意思),但是感觉 具体例子太少了~~ 难道是我没有理解透? Item 12: Understand Messag...

Effective Objective-C 2.0: Item 39: Use Handler Blocks to Reduce Code Separation

Item 39: Use Handler Blocks to Reduce Code Separation A common paradigm in programming a user int...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Effective Objective-C 2.0: Item 3: Prefer Literal Syntax over the Equivalent Methods
举报原因:
原因补充:

(最多只允许输入30个字)