Array multiplication in Objective-C, for NSArray and NSMutableArray

35 Views Asked by At

In Python, the expression 2*[a,b,c] (with a,b,c variables) is meaningful, and returns [a,b,c,a,b,c]. Also, in Python, lists (or arrays) are mutable by design.

It is easy enough to write a category on e.g. NSArray that implements an instance method -(NSArray*) multiplyByNumber:(NSInteger)number; which copies Python's behavior. But the mutability is not given in Objective-C: NSArrays are immutable by design, but they have a mutable subclass NSMutableArray, which can not really inherit the multiplyByNumber method.

Is there a way to avoid reimplementation of the method for NSMutableArray? Or do I really have to do it for the mutable subclasses as well?

0

There are 0 best solutions below