这些实际上编译为相同的程序集:
NSNumber *value = @42;
NSNumber *otherValue = @(42);
这导致以下优化的装配:
// NSNumber *value = @42;
movl L_OBJC_CLASSLIST_REFERENCES_$_-L0$pb(%edi), %eax <-- NSNumber
movl L_OBJC_SELECTOR_REFERENCES_-L0$pb(%edi), %ecx <-- numberWithInt:
movl %ecx, 4(%esp)
movl %eax, (%esp)
movl $42, 8(%esp) <-- Here's your value
calll L_objc_msgSend$stub
// NSNumber *value = @(42);
movl L_OBJC_CLASSLIST_REFERENCES_$_-L0$pb(%edi), %eax <-- NSNumber
movl L_OBJC_SELECTOR_REFERENCES_-L0$pb(%edi), %ecx <-- numberWithInt:
movl %ecx, 4(%esp)
movl %eax, (%esp)
movl $42, 8(%esp) <--- and here again
calll L_objc_msgSend$stub
值得注意的是,它甚至足够聪明,可以为您进行预计算:
// NSNumber *value = @(42 + 1);
movl L_OBJC_CLASSLIST_REFERENCES_$_-L0$pb(%edi), %eax <-- NSNumber
movl L_OBJC_SELECTOR_REFERENCES_-L0$pb(%edi), %ecx <-- numberWithInt:
movl %ecx, 4(%esp)
movl %eax, (%esp)
movl $43, 8(%esp) <--- Hey; it's precomputed to 43. Nice.
calll L_objc_msgSend$stub
正如@Zaph 所说,这对编译器无关紧要。