views:

60

answers:

1

Hi,

I'm fairly new to Objective-C and wondering if it's possible to type objects as their supertype without receiving compiler warnings when assigning them, or if there is a recognised way of achieving the same thing?

I realise that this is what type id is for but I have a base class with synthesized properties and if I try to use id I get a build error "request for member 'x' in something not a structure or union", presumably because dynamic typing is fine for sending messages to an object but not for accessing synthesized properties.

For example in Java I might have:

public abstract class A {  
     public function doSomething() {
  //some func
     }
}

public class B extends A {
    public function doSomething() {
 //override some func
    }
}

public class C extends A {
    public function doSomething() {
 //override some func
    }
}

//and in my main class: 

A objB = new B();
A objC = new C();

//the purpose of all of this is so I can then do:

A objHolder;
objHolder = objB;
objHolder.doSomething();
objHolder = objC;
objHolder.doSomething();

I currently have the above working in Objective-C but with a compiler warning: "assignment from distinct Objective-C type"

OK, here is the Objective-C interfaces, I can add the implementations if you want. It's a composite pattern:

//AbstractLeafNode

#import <Foundation/Foundation.h>

@interface AbstractLeafNode : NSObject {
    NSString* title;
    AbstractLeafNode* parent;
}

@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) AbstractLeafNode* parent;

@end

//Page

#import "AbstractLeafNode.h"

@interface Page : AbstractLeafNode {
    //there will be stuff here later!
}

@end

//Menu

#import "AbstractLeafNode.h"

@interface Menu : AbstractLeafNode {
 NSMutableArray* aChildren;
}

- (void)addChild:(AbstractLeafNode *)node;
- (void)removeChild:(AbstractLeafNode *)node;
- (AbstractLeafNode *)getChildAtIndex:(NSUInteger)index;
- (AbstractLeafNode *)getLastChild;
- (NSMutableArray *)getTitles;

@end

// I'd then like to do something like (It works with a warning):

AbstractLeafNode* node;
Menu* menu = [[Menu alloc] init];
Page* page = [[Page alloc] init];
node = menu;
[node someMethod];
node = page;
[node someMethod];

// Because of the synthesized properties I can't do this:
id node;

// I can do this, but I suspect that if I wanted synthesized properties on the page or menu it would fail:
node = (AbstractLeafNode*)menu;
node = (AbstractLeadNode*)page;
+1  A: 

Sorry, as I was editing the question I realised that I was trying to do it the wrong way round and assign an AbstractLeafNode to a Menu, so the compiler warning completely makes sense. No errors when assigning a Menu to an AbstractLeafNode.

I've been staring at this for too long!

blask