iDevGames Forums
Subclass access warnings in Objective-C - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Subclass access warnings in Objective-C (/thread-755.html)



Subclass access warnings in Objective-C - vectorscope - Sep 14, 2009 10:37 PM

Hi,

I am having some potential issues with my code where I am trying to use a getter in a subclass. Basically the code runs normally but I do get a warning during compilation. I am wondering if there is a better way to do this or do I just have to live with the warning.

Here is what is going on:

Superclass:

Code:
@interface SuperObject : NSObject
{
    NSString *name;
}

@property (nonatomic, assign) NSString *name;

@end

Subclass:

Code:
@interface SubObject : SuperObject
{
    int value;
}

@property (nonatomic, assign) int value;

@end

So if I create an array of SubObjects:

Code:
NSMutableArray *myArray = [[NSMutableArray alloc] init];

SubObject *sub;

sub = [[SubObject alloc] init];
[myArray addObject:sub];
[sub release];

sub = [[SubObject alloc] init]
[myArray addObject:sub];
[sub release];

And then I try to use that array:

Code:
for (SuperObject *obj in myArray)
{
    NSLog(@"Value %d", [obj value]);
}

When I compile this I get a warning saying "warning: no '-value' method found"

When I run it it works and does print out the right value...

Even if I change it to this:

Code:
for (SubObject *obj in myArray)
{
    NSLog(@"Value %d", [obj value]);
}

I still get the warning... Mad

Any insight would be greatly appreciated.

Best regards.


Subclass access warnings in Objective-C - SethWillits - Sep 14, 2009 11:35 PM

SuperObject doesn't define -value, so calling -value on a SuperObject is going to give you a warning. That is a good and expected thing.

Solution? Either use the type "id" instead of SuperObject in that spot* or use SubObject.

You're using SubObject, but still getting the error. This pretty much guarantees that you forgot to #import "SubObject.h"


* If you use "id", you still need to have imported a header that contains a declaration for -value. -value also needs to be a unique selector for its param/return types otherwise you'll get a warning about multiple methods. If you get that, then you'll need to typecast the id reference to a concrete type to get rid of the warning anyway, so you'd just end up using SubObject anyway.


Subclass access warnings in Objective-C - vectorscope - Sep 15, 2009 10:02 AM

Yes that was exactly it, the darn .h file wasn't being imported. Not sure why anything worked...

Thanks again.