68

Is there a way to force NSMutableArray to hold one specific object type only?

I have classes definitions as follow:

@interface Wheel:NSObject  
{    
  int size;  
  float diameter;  
}  
@end  


@interface Car:NSObject  
{  
   NSString *model;  
   NSString *make;  
   NSMutableArray *wheels;  
}  
@end

How can I force wheels array to hold Wheel objects only with code? (and absolutely not other objects)

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
Tuyen Nguyen
  • 4,159
  • 7
  • 45
  • 76
  • possible duplicate of [Is there any way to enforce typing on NSArray, NSMutableArray, etc.?](http://stackoverflow.com/questions/649483/is-there-any-way-to-enforce-typing-on-nsarray-nsmutablearray-etc) – JosephH Aug 11 '15 at 12:24

12 Answers12

97

Update in 2015

This answer was first written in early 2011 and began:

What we really want is parametric polymorphism so you could declare, say, NSMutableArray<NSString>; but alas such is not available.

In 2015 Apple apparently changed this with the introduction of "lightweight generics" into Objective-C and now you can declare:

NSMutableArray<NSString *> *onlyStrings = [NSMutableArray new];

But all is not quite what it seems, notice the "lightweight"... Then notice that the initialisation part of the above declaration does not contain any generic notation. While Apple have introduced parametric collections, and adding a non-string directly to the above array, onlyStrings, as in say:

[onlyStrings addObject:@666]; // <- Warning: Incompatible pointer types...

will illicit the warning as indicated, the type security is barely skin deep. Consider the method:

- (void) push:(id)obj onto:(NSMutableArray *)array
{
   [array addObject:obj];
}

and the code fragment in another method of the same class:

NSMutableArray<NSString *> *oops = [NSMutableArray new];
[self push:@"asda" onto:oops]; // add a string, fine
[self push:@42 onto:oops];     // add a number, no warnings...

What Apple have implemented is essentially a hinting system to assist with automatic inter-operation with Swift, which does have a flavour of type-safe generics. However on the Objective-C side, while the compiler provides some extra hints the system is "lightweight" and type-integrity is still ultimately down to the programmer - as is the Objective-C way.

So which should you use? The new lightweight/pseudo generics, or devise your own patterns for your code? There really is no right answer, figure out what makes sense in your scenario and use it.

For example: If you are targeting interoperation with Swift you should use the lightweight generics! However if the type integrity of a collection is important in your scenario then you could combine the lightweight generics with your own code on the Objective-C side which enforces the type integrity that Swift will on its side.

The Remainder of the 2011 Answer

As another option here is a quick general subclass of NSMutableArray which you init with the kind of object you want in your monomorphic array. This option does not give you static type-checking (in as much as you ever get it in Obj-C), you get runtime exceptions on inserting the wrong type, just as you get runtime exceptions for index out of bounds etc.

This is not thoroughly tested and assumes the documentation on overriding NSMutableArray is correct...

@interface MonomorphicArray : NSMutableArray
{
    Class elementClass;
    NSMutableArray *realArray;
}

- (id) initWithClass:(Class)element andCapacity:(NSUInteger)numItems;
- (id) initWithClass:(Class)element;

@end

And the implementation:

@implementation MonomorphicArray

- (id) initWithClass:(Class)element andCapacity:(NSUInteger)numItems
{
    elementClass = element;
    realArray = [NSMutableArray arrayWithCapacity:numItems];
    return self;
}

- (id) initWithClass:(Class)element
{
    elementClass = element;
    realArray = [NSMutableArray new];
    return self;
}

// override primitive NSMutableArray methods and enforce monomorphism

- (void) insertObject:(id)anObject atIndex:(NSUInteger)index
{
    if ([anObject isKindOfClass:elementClass]) // allows subclasses, use isMemeberOfClass for exact match
    {
        [realArray insertObject:anObject atIndex:index];
    }
    else
    {
        NSException* myException = [NSException
            exceptionWithName:@"InvalidAddObject"
            reason:@"Added object has wrong type"
            userInfo:nil];
        @throw myException;
    }
}

- (void) removeObjectAtIndex:(NSUInteger)index
{
    [realArray removeObjectAtIndex:index];
}

// override primitive NSArray methods

- (NSUInteger) count
{
    return [realArray count];
}

- (id) objectAtIndex:(NSUInteger)index
{
    return [realArray objectAtIndex:index];
}


// block all the other init's (some could be supported)

static id NotSupported()
{
    NSException* myException = [NSException
        exceptionWithName:@"InvalidInitializer"
        reason:@"Only initWithClass: and initWithClass:andCapacity: supported"
        userInfo:nil];
    @throw myException;
}

- (id)initWithArray:(NSArray *)anArray { return NotSupported(); }
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag { return NotSupported(); }
- (id)initWithContentsOfFile:(NSString *)aPath { return NotSupported(); }
- (id)initWithContentsOfURL:(NSURL *)aURL { return NotSupported(); }
- (id)initWithObjects:(id)firstObj, ... { return NotSupported(); }
- (id)initWithObjects:(const id *)objects count:(NSUInteger)count { return NotSupported(); }

@end

Use as:

MonomorphicArray *monoString = [[MonomorphicArray alloc] initWithClass:[NSString class] andCapacity:3];

[monoString addObject:@"A string"];
[monoString addObject:[NSNumber numberWithInt:42]]; // will throw
[monoString addObject:@"Another string"];
CRD
  • 50,500
  • 4
  • 64
  • 80
  • Thanks, that's what I'm looking for. – Tuyen Nguyen Mar 04 '11 at 19:55
  • +1 — based on this code I got the idea to use blocks to test, if a certain object should be added to an array. This can be a membership for a class, a value for a property of the object or virtual anything else. Please have a look: http://stackoverflow.com/questions/8190530/does-objective-c-support-generics/8198852#8198852 – vikingosegundo Nov 20 '11 at 01:59
  • @bendytree - using macros to generate one class per type for static typing checking is an interesting idea. However you don't need the forward invocation stuff at all, just subclass NSMutableArray properly as in the above example and it will be much more efficient. So combine your macros with the above and you'll get the best of both. – CRD Jan 08 '13 at 08:49
  • @CRD I am facing problem where I need to check if object to be added to array is implementing a delegate or not... – NetDeveloper Aug 29 '13 at 17:22
  • @Jignesh - You can use the same outline but instead of testing for class membership you'd be testing for protocol conformance or selector implementation. If that doesn't make sense post a question (not another comment). – CRD Aug 29 '13 at 20:49
  • Why do you still need a NSMutableArray realArray in your subclass. MonomorphicArray IS an NSMutableArray.. Can't you call the super method of the parent class ? – Paul Praet Nov 20 '13 at 09:01
  • 3
    @PaulPraet - `NSArray` and `NSMutableArray` are a *class cluster* and the way they are designed any subclass must provide its own storage for the elements - which can be a separate instance of `NSArray`/`NSMutableArray` as used here. See "Subclassing Notes" in the [`NSArray`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html) documentation. – CRD Nov 20 '13 at 09:21
  • Reluctantly voting down since this answer is now outdated and incorrect. I hope OP changes the accepted answer. – Ben Leggiero Dec 30 '15 at 23:14
  • 1
    @BenC.R.Leggiero - The answer isn't "incorrect", indeed if you want a monomorphic array the answer gives you that at runtime; while the new lightweight generics get you closer, but not all the way there, at compile time. Yes language changes means it is a bit "outdated" as it doesn't cover stuff that has happened since it was written, I've edited to add some details (as doing so has some merit in this case) but you'll often find stuff on SO whose veracity time has changed. If providing answers incurred editing in perpetuity there would be far fewer answers! There can be value in history. :-) – CRD Dec 31 '15 at 08:47
  • @CRD Thank you so much for providing the modern approach; I voted it back up! I agree there's value in history, but indeed answering on SO implies a dedication to ensuring your answer is up-to-date, since no one is allowed to ask a question that's already been asked, and your Google rank will likely only climb. – Ben Leggiero Jan 04 '16 at 21:51
30

With XCode 7 generics are now available in Objective-C!

So you can declare your NSMutableArray as:

NSMutableArray <Wheel*> *wheels = [[NSMutableArray alloc] initWithArray:@[[Wheel new],[Wheel new]];

The compiler will give you a warning if you try to put non-Wheel object in your array.

andreacipriani
  • 2,450
  • 23
  • 23
  • I think you've forgotten an `*` before wheels. – NKorotkov Oct 12 '15 at 12:49
  • 1
    Don't I need to say `[[NSMutableArray alloc] init...]`? – Ben Leggiero Dec 30 '15 at 23:14
  • 1
    We might all want parametric types, unfortunately we need to read Apple's small print; these are what Apple term "lightweight" generics and essentially only exist as comments to assist the Swift compiler. They unfortunately do not always "give you a warning" if you attempt an ill-typed operation, I've updated my answer to provide some more detail. – CRD Dec 31 '15 at 08:30
10

I could be wrong (I'm a noob), but I think, if you create a custom protocol and make sure the objects you are adding to the array follow the same protocol, then when you declare the array you use

NSArray<Protocol Name>

That should prevent objects being added that do not follow the said protocol.

Gravedigga
  • 197
  • 2
  • 7
  • 3
    Why answer this if you don't know it? – Roel Jul 11 '13 at 09:55
  • 20
    It's known as attempting to help fellow programmers. – Gravedigga Jul 12 '13 at 12:25
  • 1
    This is wrong, this type allows you only to assign subclasses of NSArray that conform to `Protocol Name`. So it's related to the array object itself and not its contents. You should write `NSArray>` to achieve your initial goal. – DanSkeel Nov 06 '18 at 14:07
5

as per i know.. before you added any object in wheels mutableArray, u have to add some check mark. Is the object which i am adding is class "wheel". if it is then add, other wise not.

Example:

if([id isClassOf:"Wheel"] == YES)
{
[array addObject:id) 
}

Something like this. i dont remember the exact syntax.

vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
harshit2811
  • 757
  • 1
  • 5
  • 20
  • 2
    I agree - don't subclass nsmutablearray - just hide it in your car, and make the only two methods addWheel:(Wheel*)aWheel and deleteWheel:. (for example). – Tom Andersen Mar 04 '11 at 18:20
  • When I say hide the wheel array, I mean make it private, don't make it a property, etc. You might also need a -(NSArray*)wheels; call, which does this return [NSArray arrayWithArray:wheels]; – Tom Andersen Mar 04 '11 at 18:22
3

I hope this will help (and work... :P )

Wheel.h file:

@protocol Wheel
@end

@interface Wheel : NSObject
@property ...
@end

Car.h file:

#import "Wheel.h"
@interface Car:NSObject  

{  
   NSString *model;  
   NSString *make;  
   NSMutableArray<Wheel, Optional> *wheels;  
}  
@end

Car.m file:

#import "Car.h"
@implementation Car

-(id)init{
   if (self=[super init]){
   self.wheels = (NSMutableArray<Wheel,Optional>*)[NSMutableArray alloc]init];
   }
return self;
}
@end
Aviram Netanel
  • 10,273
  • 7
  • 36
  • 63
2

Xcode 7 allows you to define Arrays, Dictionaries, and even your own Classes as having generics. The array syntax is as follows:

NSArray<NSString*>* array = @[@"hello world"];
Brian Trzupek
  • 4,294
  • 1
  • 14
  • 17
1

I don't believe there's any way to do it with NSMutableArray out of the box. You could probably enforce this by subclassing and overriding all the constructors and insertion methods, but it's probably not worth it. What are you hoping to achieve with this?

Jim
  • 69,259
  • 14
  • 96
  • 104
0

You can use the nsexception if you dont have the specific object.

for (int i = 0; i<items.count;i++) {
 if([[items objectAtIndex:i] isKindOfClass:[Wheel class]])
 {
  // do something..!
 }else{
  [NSException raise:@"Invalid value" format:@"Format of %@ is invalid", items];
  // do whatever to handle or raise your exception.
 }
}
Lalith B
  • 10,975
  • 4
  • 27
  • 47
0

Here's something I've done to avoid subclassing NSMutableArray: use a category. This way you can have the argument and return types you want. Note the naming convention: replace the word "object" in each of the methods you will use with the name of the element class. "objectAtIndex" becomes "wheelAtIndex" and so on. This way there's no name conflict. Very tidy.

typedef NSMutableArray WheelList;
@interface NSMutableArray (WheelList) 
- (wheel *) wheelAtIndex: (NSUInteger) index;
- (void) addWheel: (wheel *) w;
@end

@implementation NSMutableArray (WheelList)

- (wheel *) wheelAtIndex: (NSUInteger) index 
{  
    return (wheel *) [self objectAtIndex: index];  
}

- (void) addWheel: (wheel *) w 
{  
    [self addObject: w];  
} 
@end


@interface Car : NSObject
@property WheelList *wheels;
@end;


@implementation Car
@synthesize wheels;

- (id) init 
{
    if (self = [super init]) {
        wheels = [[WheelList alloc] initWithCapacity: 4];
    }
    return self;
}

@end
  • While this works to give you the types it doesn't actually restrict the contents of the array to be the type as you can just call `addObject:` directly - which means a subsequent `wheelAtIndex:` could be casting something other than a wheel as a wheel and chaos will ensue. You can combine your approach with `MonomorphicArray` from the other answer; just extend `MonomorphicArray` with a class with typed getters and setters. – CRD Feb 21 '13 at 21:46
0

protocol maybe a good idea:

@protocol Person <NSObject>
@end

@interface Person : NSObject <Person>
@end

to use:

NSArray<Person>*  personArray;
lbsweek
  • 4,230
  • 37
  • 37
  • Have you test your code?? why NSArray conform Person protocol can do the check? – johnMa Jan 05 '14 at 09:49
  • the compiler would only give a warning if your object did not conform to the protocol. – lbsweek Jan 08 '14 at 16:04
  • 2
    No, it won't, In fact you will get `Incompatible pointer types initializing 'NSArray *' with an expression of type 'NSArray *'`,even when you insert Person in it. – johnMa Jan 09 '14 at 00:59
0

There is one-header file project which allows this: Objective-C-Generics

Usage:

Copy ObjectiveCGenerics.h to your project. When defining a new class use the GENERICSABLE macro.

#import "ObjectiveCGenerics.h"

GENERICSABLE(MyClass)

@interface MyClass : NSObject<MyClass>

@property (nonatomic, strong) NSString* name;

@end

Now you can use generics with arrays and sets just as you normally do in Java, C#, etc.

Code: enter image description here

Misha
  • 4,912
  • 5
  • 33
  • 59
0

That's not possible; an NSArray (whether mutable or not) will hold any object type. What you can do is to create your own custom subclasses as already suggested by Jim. Alternatively, if you wanted to filter an array to remove objects that weren't of the type you want, then you could do:

- (void)removeObjectsFromArray:(NSMutableArray *)array otherThanOfType:(Class)type
{
    int c = 0;
    while(c < [array length])
    {
        NSObject *object = [array objectAtIndex:c];
        if([object isKindOfClass:type])
          c++;
        else
          [array removeObjectAtIndex:c];
    }
}

...
[self removeObjectsFromArray:array otherThanOfType:[Car class]];

Or make other judgments based on the result of isKindOfClass:, e.g. to divide an array containing a mixture of Cars and Wheels into two arrays, each containing only one kind of object.

Tommy
  • 97,164
  • 12
  • 174
  • 193