VC# vs. ObjC and partial abstract classes
VC# vs. ObjC and partial abstract classes
- Subject: VC# vs. ObjC and partial abstract classes
- From: William Squires <email@hidden>
- Date: Tue, 18 Mar 2014 19:30:30 -0500
Hi all!
Some languages (like C++ and Visual C#) allow for partial abstract classes (i.e. some methods are implemented, while others are left to subclasses to implement - and, in fact, must implement since the partial abstract class does not). Is there a way to do this in ObjC?
Is this why NSObject implements a protocol called NSObject (i.e. to make NSObject a partial abstract class)?
Finally, does anyone know of a tool that'll convert VC# (dot net) code to ObjC (modern 2.0 syntax)?
Can one forward declare an @protocol?
Obviously (IIRC) a pure abstract class would map to a formal protocol in ObjC (or a class interface in languages such as REALbasic/Xojo, or VB 6). My best guess is to:
1) Make an ObjC class, and have it implement those methods that subclasses don't have to override. For those the subclasses must override, implement a stub that raises an exception if a message is sent to it (as ObjC doesn't have an "abstract" keyword).
2) Create a ObjC protocol with the same name as the above class, and copy/paste the method prototypes from the ObjC class' .h file into the protocol's .h file.
3) Go back to the ObjC class, and have it implement its own interface.
Ex: Let's say I have a class, MyClass, that has two methods; one has a base implementation (albeit simple), and the other is abstract (i.e. subclasses must implement it).
"MyClass.h"
@protocol MyClass; // Is this a legal forward protocol declaration?
@interface MyClass : NSObject <MyClass>
-(void)aSimpleMethod;
-(void)subclassesMustImplementMe;
@end
"MyClassProtocol.h"
@protocol MyClass <NSObject>
-(void)aSimpleMethod;
-(void)subclassesMustImplementMe;
@end
"MyClass.m"
#import <Foundation/Foundation.h>
#import "MyClassProtocol.h"
#import "MyClass.h"
@implementation MyClass
-(void)aSimpleMethod
{
// Subclasses don't have to override me
NSLog(@"%@\n", [NSString stringFromClass:[self class]]);
}
-(void)subclassesMustImplementMe
{
// Throw an exception here!
}
@end
Is there a better way to implement this than the above?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden