Copy and mutableCopy are two methods involved in copying objects. If a custom object uses these two methods, it must comply with the NSCopying and NSMutableCopying protocols. And implement their corresponding methods copyWithZone: and mutableCopyWithZone: through the runtime source nsobject. mm, you can understand the implementation of both as follows:

+ (id)copyWithZone:(struct _NSZone *)zone {
    return (id)self;
}

- (id)copy {
    return [(id)self copyWithZone:nil];
}

+ (id)mutableCopyWithZone:(struct _NSZone *)zone {
    return (id)self;
}

- (id)mutableCopy {
    return [(id)self mutableCopyWithZone:nil];
}
Copy the code

The copy and mutableCopy methods simply call the difference between copyWithZone: and mutableCopyWithZone:

The copy method is used to copy a copy of an object. In general, the copy method always returns an immutable copy of an object, even if the object itself is modifiable. For example, NSMutableString calls the copy method, which returns an immutable string object. 2. The mutableCopy method is used to copy mutable copies of objects. In general, the mutableCopy method always returns a modifiable copy of an object, even if the copied object itself is not modifiable.