Tutorial :NSMutableArray with only a particular type of objects



Question:

is it possible to specify that a NSMutableArray can only contain a certain type of objects. For example, if I want to store only this kind of objects :

@interface MyObject : NSObject {      UInt8 value;   }  

In order to be able to use the instance variable like this :

- (void)myMethod:(NSMutableArray *)myArray{      for (id myObject in myArray){          [self otherMethod:myObject.value];      }  }  

because I'm getting this error :

request for member 'value' in something not a structure or union  

Thank you for your help


Solution:1

You are getting that error, because for as far as Objective-C is concerned, myObject is of the non-type id, which doesn't support the value property. To make Objective-C aware of the fact it's always dealing with a MyObject in this loop, you'll have to tell it the myObject object is an instance of MyObject.

for (MyObject *myObject in myArray) {  

Also, you have to make sure the value ivar is accessible using dot-notation by implementing getter and setter methods for it. You can do this yourself by implementing -value and -setValue:, or you can use @property and @synthesize to let Objective-C do this.


Solution:2

It sounds like you're coming from a Java/C# type background where limits can be imposed on collections.

Collections in Cocoa don't follow that pattern. There is no way to set a restriction on what type of objects can be inserted (unless you write a wrapper class that enforces this).

Objective-C, by design, follows the "if it walks like a duck and it quacks like a duck, then it most probably is a duck" philosophy. That is to say that rather than checking whether an object is a particular type, you should be checking whether it can do what you want it to do regardless of its type.

You can do this using respondsToSelector:.

Finally, your problem isn't actually related to the fact that the array has no restrictions. Your object doesn't appear to declare the instance variable value as a property, or expose any accessor methods for it.

This is why you're seeing the error when you try myObject.value. That syntax in Objective-C is how you access properties.

The default scope for instance variables in Objective-C is @protected, which means anything outside your class can't access them without going through an accessor method of some kind.

You need to declare and define the methods - (UInt8)value and - (void)setValue:(UInt8)aValue and use them.

Alternatively, you could declare it as a property.


Solution:3

Objective-C doesn't work like that. You need to use [myObject value] (which will work irrespective of the kind of object, as long as it responds to -[value]. If you only want one type of objects in it, insert only that type of objects.


Solution:4

You would have to write a wrapper-class for the NSMutableArray, see for example this question.


Solution:5

Subclass NSMutableArray and override methods that mediate the addition of objects to the array. You would check the object type in these overridden methods, only calling [super addObject:xyz] if the type is accepted.


Solution:6

maybe you can use protocol:

@protocol Person <NSObject>  @end    @interface Person : NSObject <Person>  @end  

to use:

NSArray<Person>*  personArray;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »