0

I am trying to introduce some generics-style approaches from .NET into my iOS development. I am retrieving a list of custom objects of type Example.

I have defined a protocol that Example class implements:

@protocol ExampleProtocol
@property(nonatomic,assign) int Id;
@property(nonatomic,copy)NSString *Description;
@optional
@property(nonatomic,copy)NSString *Icon;
@end

I then retrieve my NSMutableArray of Example as follows:

id<ExampleProtocol> anExample = [arrayOfExampleProtocols objectAtIndex:0];

The problem I have is that anExample is always empty regardless of the contents of the NSMutableArray. My ultimate aim is to be able to reference the properties with the following syntax:

id<ExampleProtocol> anExample = [arrayOfExampleProtocols objectAtIndex:0];
NSString *test = [anExample Description];
JordanMazurke
  • 1,103
  • 10
  • 22
  • 4
    I am willing to put money on the fact that you never initialized your mutable array because if `anExample` were "empty" (I'm assuming you mean `nil`) your code would crash due to an index out of range exception. – Joe Aug 17 '12 at 20:56

1 Answers1

0

There are no generics in Obj-C. The "protocol" is a messaging mechanism. Please see this post: No Generics in Obj-C

Community
  • 1
  • 1
TrevorL
  • 495
  • 2
  • 7