2

I have created an object of type NSMutableArray

#import <Foundation/Foundation.h>

@interface MyCustomObject : NSMutableArray
{ 
}

@end

in one of my classes, I delcare an instance:

MyCustomObject *myObj = [[NSMutableArray alloc] init];

But xcode is giving a warning on this line: Incompatible pointer types initializing 'MyCustomObject *' with an expression of type 'NSMutableArray *'

Any idea what is wrong? It works fine, but just wondering why it is throwing a warning and how to resolve it?

2
  • xcode is just an IDE, and Xcode NSMutableArray is not very descriptive. Commented Oct 18, 2011 at 19:13
  • 1
    You need to consider marking one of these answers as accepted. Commented Oct 19, 2011 at 4:26

6 Answers 6

3

You can assign a child class to a variable typed as its super-class, but you cannot assign a super-class to a child class variable.

So, you'd want to:

MyCustomObject *myObj = [[MyCustomObject alloc] init];
Sign up to request clarification or add additional context in comments.

Comments

3

Firstly, as everyone else has said, you need to instantiate your class if you want an instance of your class. Evidently the reason you're not doing this is because you tried instantiating your class and it didn't work. That's the deeper problem: You can't simply subclass NSArray or NSMutableArray. As noted in the documentation, NSArray is a class cluster, which is basically a short way of saying "NSArray doesn't actually implement most of its methods."

In order to subclass NSArray, you essentially have to provide all of its functionality yourself. It is generally much easier to either create a category on NSArray or create a custom class that has an array as a member.

Comments

2

I think you've got things backwards. MyCustomObject is a NSMutableArray, but NSMutableArray isn't a MyCustomObject. Your variable myObj should be a NSMutableArray if it needs to be able to hold both NSMutableArrays and MyCustomObjects.

3 Comments

All I am doing is creating a named object (class) as a collection. So lets say MyCustomObject is really Cars and I have another object Car with actual properties like name, color, etc
Sure, but the compiler doesn't know that. As a class, MyCustomObject could have fields that NSMutableArray lacks. If you have a pointer to a MyCustomObject, but it actually points to an NSMutableArray, then the compiler will allow you to touch the nonexistent field. Boom!
In that case you do NSMutableArray *cars = [[NSMutableArray alloc] init]; and then add Car instances to this array.
2

You should be instantiating the object as:

MyCustomObject *myObj = [[MyCustomObject alloc] init];

Otherwise, all you're doing is making an NSMutableArray and don't need your custom object (since none of its functionality would work.)

2 Comments

As I said above, when I instantiate it this way and do addObject it throws a SIGABT
My guess is that we'd need to see more of your custom object's code to see what's happening.
2

It should be:

MyCustomObject *myObj = [[MyCustomObject alloc] init];

if you do:

NSMutableArray *myObj = [[MyCustomObject alloc] init];

It will create an istance of MyCustomObject, but you will have a NSMutableArray pointer, so your new instance will just be seen as a NSMutableArray.

In this case:

MyCustomObject *myObj = [[NSMutableArray alloc] init];

It is an error. In previows a MyCustomObject can ben seen as a simple NSMutableArray. but a NSMutableArray cannot ever be seen as a MyCustomObject. It is a simple inheritance property.

Maybe this can help

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

8 Comments

and a class should not be called Object
The thing is, when I initialize it this way, when I add an object to it, I get SIGABT. If I change it back to [[NSMutableArray alloc] init] it works fine.
Its not actually called that, I just put that for the example...but really it can be called whatever.
Do you have any more field or methods, or is just empty? if it is, I assume that you want NSMutableArray *myObj = [[NSMutableArray alloc] init]; and forget about MyCustomObject.
Yes, however I need to refer to my custom object throughout the app. In my appdelegate I refer to this object. Lets call it Cars
|
0

Try allocating a MyCustomObject instead:

MyCustomObject *myObj = [[MyCustomObject alloc] init];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.