views:

51

answers:

1

NSStreamDelegate was defined in previous OS as (NSObject)NSStreamDelegate In the latest OS it is defined as id

Both have the same function.

If I want to write code that is Runtime system aware. How do I create an object that is both and neither? I dream of that truly universal app.

if (catchOS10.5_or_iOS3.2) { [MyStream setDelegate:myObj] } else { [MyStream setDelegate:myObjWithProtocol] }

I have myHandlerClass which in the NEW os is MyClass:NSObject

Thus my quandary.

Any suggestions?

-A

A: 

Do you actually have trouble getting this to work on both versions? The two ideas are basically the same.

You will definitely have to declare your delegate class as implementing the NSStreamDelegate protocol (which is a formal protocol, not an informal one, in the current SDK):

@interface MyHandlerClass : NSObject <NSStreamDelegate> {
    // ...
}
-(void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent;
@end

Since the "id" type is really just a typedef for a pointer to an Obj-C object, your pointer to your delegate class will both be an id as well as an NSObject:

NSStream *myStream = [[NSStream alloc] init];
MyHandlerClass *del = [[MyHandlerClass alloc] init];
myStream.delegate = del;

... should work on both SDKs. Or, if you're creating your stream inside of your delegate class (a common idiom) you'd do:

NSStream *myStream = [[NSStream alloc] init];
myStream.delegate = self;

to the same happy end.

Seamus Campbell