Hacker News new | ask | show | jobs
by thought_alarm 698 days ago
Historical fun fact:

In the original version of Objective-C and NextStep (1988-1994), the common base class (Object) provided an implementation of `copyFromZone:` that did an exact memcpy of the object, a la NSCopyObject. In other words, NSCopyObject was the default behavior for all Obj-C objects.

It was still up to each subclass to ensure that copyFromZone: worked correctly with its own data (not all classes supported it).

AppKit's `Cell` class provided this implementation:

    - copyFromZone:(NXZone *)zone
    {
        Cell *retval;
        retval = [super copyFromZone:zone];
        if (cFlags1.freeText && contents) 
            retval->contents = NXCopyStringBufferFromZone(contents, zone);
        return retval;
    }
Here it needs to make a copy of its `contents` string, using NXCopyStringBufferFromZone, when the copy of Cell is expecting to free that memory (cFlags1.freeText).

OpenStep introduced reference counting and the NSCopying protocol, and removed the `copyWithZone:` implementation in NSObject.

So the equivalent implementation in OpenStep's NSCell class could be:

    - (id)copyWithZone:(NSZone *)zone
    {
        NSCell *retval;
        retval = NSCopyObject(self, 0, zone);
        [retval->contents retain];
        return retval;
    }