0

I know that the question title is not perfectly clear but I don't know how to better explain my doubt.

I found a similar question here but my problem is slightly different:

If I use "generic" id then I lose information about the object I put in the variable and I don't have compile time issue about wrong data types, so I need to change id to NSString for specific classes.

The point is: Protocols are meant to be generic, so, if I define a protocol with this property:

@protocol MyProtocol
   @property (nonatomic,strong) id aVariable;
@end

Then I create a class like this:

.h

@interface MyClass : NSObject <MyProtocol>
[...]

.m

@implementation MyClass
  @syntetise aVariable = _aVariable;
  [...]
@end

The question is: Since I want that the class "MyClass" has a specific type for aVariable (example NSString) how can I change the type of the protocol's variable implementation for specific classes?

I hope to be more clear with the question now. :)

Community
  • 1
  • 1
GrizzlyBear
  • 1,098
  • 1
  • 13
  • 27
  • 1
    I don't think protocols are meant to be generic. A protocol defines a contract between two ore more classes. – dasdom May 05 '15 at 12:31
  • `Protocols are meant to be generic`. It's not true. objective-c doesn't have generics or something looks like generics. That's why we have just `NSArray` instead `NSArray`. If you need generics so hard, you can look at objective-c++'s templates. But you also will not be able create `NSArray`. – Cy-4AH May 05 '15 at 13:23
  • I know. I didn't write "generic" as "informatic point of view" but as "unrelated to a specific class". – GrizzlyBear May 05 '15 at 13:28

0 Answers0