> On Aug 19, 2016, at 9:56 PM, Charles Srstka <[email protected]> wrote:
> 
>>> So for the foreseeable future, the
>>> difference is that +array returns an autoreleased object, meaning that in
>>> ARC code, +new is the better choice.
>> 
>> I would not make that assumption. Who says +[NSArray array] constructs
>> anything at all? Try comparing the return values of two calls to
>> [NSArray array] sometime. ;-)
>> 
>> I happen think +new is more readable, but it’s really just a matter of
>> preference.
> 
> Just because you didn’t create a brand-new object doesn’t mean you can’t (or 
> that you shouldn’t, under pre-ARC coding conventions) do a retain-autorelease 
> dance on it.

As an aside:

#import <Foundation/Foundation.h>
#include <objc/runtime.h>

void SetUpVeryVeryBadAutoreleaseHack() {
        Class class = [[NSArray array] class]; // because it won't just be 
NSArray due to the class cluster
        SEL selector = NSSelectorFromString(@"autorelease");
        
        Method method = class_getInstanceMethod(class, selector);
        IMP oldImpl = method_getImplementation(method);
        
        IMP newImpl = imp_implementationWithBlock(^id(id s) {
                NSLog(@"NSArray %p's -autorelease called", s);
                
                return ((id (*)(id, SEL)) oldImpl)(s, selector);
        });
        
        method_setImplementation(method, newImpl);
}

int main(int argc, char *argv[]) {
        @autoreleasepool {
                // Just in case one of you complains ;-)
                NSLog(@"Do +[NSArray array] and +[NSArray new] return the same 
class: %u", [[NSArray array] class] == [[NSArray new] class]);
                
                SetUpVeryVeryBadAutoreleaseHack();
                
                NSLog(@"calling +[NSArray array]");
                NSArray *arr1 = [NSArray array];
                NSLog(@"calling +[NSArray new]");
                NSArray *arr2 = [NSArray new];
        }
}

returns:

2016-08-19 22:03:42.599 Untitled[14693:2298098] Do +[NSArray array] and 
+[NSArray new] return the same class: 1
2016-08-19 22:03:42.600 Untitled[14693:2298098] calling +[NSArray array]
2016-08-19 22:03:42.600 Untitled[14693:2298098] NSArray 0x7fbdf2400180's 
-autorelease called
2016-08-19 22:03:42.600 Untitled[14693:2298098] calling +[NSArray new]

Could this change in the future? Theoretically, but it’s unlikely, since 1) 
it’s entirely possible that doing so could break binary compatibility, causing 
crashes if any app accessed the array at a time where it would have been 
deallocated had it not been in the autoreleasepool, and 2) there’d be very 
little benefit to doing so, since eventually we’re going to get to a point 
where most new code is in Swift, which won’t access either of these 
initializers.

Charles

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to