Changing superclass designation in Xcode on the fly

In a programming exercise, I am to change the a class's superclass name. But, if I go into the interface file and change the superclass name from NSObject to a different name, Xcode gives me an error. At the time when the .h and .m files were created, you had to designate the class name these files would be a subclass to. Apparently, this is set into stone. I cannot find anything to override or change the superclass designation from any of the menu bar selections. I know you can move items and variables up and down the class hierarchy but I want to move two files. So far the only solution I have found is to delete both files and create them again with a new subclass designation. Surely, there is more flexibility in this IDE; because during a project development, changes constantly occur.


The file creating procedure was to select File, New File, Cocoa Touch under iOS, select Objective-C Class. Another window pops up and you enter your class name and then select which class this new class is a subclass to.



Example: Rectangle.h

@interface Rectangle: NSObject - orginally designated when Rectangle.h and Rectangle.m files were created.


@interface Rectangle: GraphicObject - typed over the above class to this, but Xcode displays an error because when the Rectangle.h and Rectangle.m files were created, Rectangle was set to be the subclass of NSObject.

Xcode-OTHER, OS X Mountain Lion (10.8.2)

Posted on Mar 16, 2013 8:42 AM

Reply
8 replies

Mar 16, 2013 9:25 AM in response to todrico

There use to be a method on Object class_poseAs: that would let you switch one class for another but that's been depricated for a while now. Not sure if there is a replacement.


You could try messimg with the instance isa pointer. From the manual


The isa Pointer

Every object has an

isa
instance variable that identifies the object's class. The runtime uses this pointer to determine the actual class of the object when it needs to.


Message was edited by: Frank Caggiano - there is also the method objc_setClass which you might want to look at

Mar 16, 2013 1:41 PM in response to Frank Caggiano

Frank you are a life saver. And I don't mean the kind you find in a package. Can you please type up an object_setClass example from examples in my original post? I would like to see an example of changing Rectangle being a subclass of NSObject to GraphicObject. Not sure how all the pieces would fit at this time. I you can do that, then I am good to go and will relay your knowledge to the "Programming in Objective-C" forum. I am sure there are several who would like to know.


P.S. Are the companion guides on-line or printed? When I clicked on you link, it just displayed the same three lines in a window. If it is on-line, what would be the link. If printed, the author. Thanks again!

Mar 19, 2013 6:33 AM in response to todrico

Turns out the solution was simple. Besides changing the superclass name I had to add an #import with that superclass header file name. What threw me was when Xcode did it automatically and thought this was locked into the system. The book hinted around the issue but I wish it was more direct.


I searced for the documentation you used as referrence but could not find it. I this something exclusive with the developer's membership? Please let me know if it is. If not, please pass on any link to the documentation. I am not yet have subscribed to the developer's membership. I wanted to wait until I got closer to publishing unless I really need to spend the $99. Thanks again for the help.

Mar 19, 2013 7:57 AM in response to todrico

Glad you were able to find a solution that worked for you. It's always a better learning experience when this happens then when a solution is given.


The documentation I referenced is the standard docs that come with Xcode. If you open the Organizer and click on Documentation and enter object_setclass in the search box you should find it.


You can also find it online here object_setClass


regards

Mar 22, 2013 10:45 AM in response to todrico

No Xcode is exclusively an Apple product and and can only run on OS X. I agree, with the latest version of Xcode Apple finally has a development environment that almost makes me forget Smalltalk 😉


I've got a couple of years on you, it's the new tricks that keep us old dogs going! Though given the ages of some of the other regulars around here we're both spring chickens.



regards

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Changing superclass designation in Xcode on the fly

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.