0

Lets assume I have Class BaseClass in BaseClass.h. I want to create a SubClass and inherit from my BaseClass , as simple as that. BUT I want to make the inheritance in the interface of SubClass.

 // SubClass.h
//#import "BaseClass.h" -I dont want to make import to the header (Better convention - I think so).
//@class BaseClass; - That will work only for declaring an instance/property.

@interface SubClass : BaseClass{

 }

I also would like to keep the both classes in separate files. Do I have a simple/elegant solution for instance to group my classes in the Xcode project so they can recognize each other.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Mike.R
  • 2,824
  • 2
  • 26
  • 34
  • Why don't you want to do the #import?? It's simple enough. Yes, you can use the .pch file, but that has a tendency to create odd bugs. – Hot Licks Apr 05 '14 at 19:40
  • Its not that I "don't want" . I just read that its better (some sort of Convention) to put all your imports in the .m file and use the @class in .h file but in that case it didn't work. So I thought maybe there is another option? or maybe not. – Mike.R Apr 05 '14 at 20:13
  • And now you've been told the answer: "not". – matt Apr 05 '14 at 20:27
  • Try this. In Xcode, in your project, make a new UIView subclass (choose File > New, iOS > Cocoa Touch > Objective-C Class, class MyView subclass of UIView. Now do the same thing again, but this time make it a MyOtherView which is a subclass of MyView. Look at OtherView.h. They have imported MyView.h! The very thing you refuse to do. And notice this: if you take out that import, the project won't compile! – matt Apr 05 '14 at 20:38
  • However, you can take the `UIKit` import out of the MyView header and everything still works. Why? Because the _.pch_ file imports `UIKit`, and therefore everything else imports it implicitly. – matt Apr 05 '14 at 22:55
  • Tnx a lot Guys, +1 for convincing me. – Mike.R Apr 06 '14 at 08:50

3 Answers3

3
//#import "BaseClass.h"

Uncomment that line. You must import the header of the superclass in order to make this a subclass of it. I don't see what your objection was to doing that.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks Matt , as I wrote in my question the only objection is to avoid declaration Cycling and as far as I now its not "very" correct to insert imports in header file. – Mike.R Apr 05 '14 at 18:59
  • I don't know what to say except to repeat my answer. Regardless of what you think you know, what I'm saying to do is what you must do. – matt Apr 05 '14 at 19:00
  • Also keep in mind that `#import` is not `#include`. It prevents circularity. – matt Apr 05 '14 at 19:01
  • I really don't know how to explain it BUT I somehow succeeded to accomplish it for other two classes (or maybe it some sort of Xcode bug) that I can see my class in another header without importing it. – Mike.R Apr 05 '14 at 19:02
  • Yes Import should avoid circularity and still a lot of people suggest NOT to import in the header. – Mike.R Apr 05 '14 at 19:04
  • Just curious if there is a another way? – Mike.R Apr 05 '14 at 19:04
  • Why better to use import in .m file- http://stackoverflow.com/questions/10531817/importing-header-in-objective-c – Mike.R Apr 05 '14 at 19:09
  • @Mike.R - I've been programming in Objective-C for 4 years and never seen anyone suggest that #import not be used in a .h file. – Hot Licks Apr 05 '14 at 19:41
  • Well, @HotLicks, he's probably thinking of the notion that one should #import in the .m file unless one _must_ #import in the .h file. But this is one of the cases in which one must. :) – matt Apr 05 '14 at 19:55
  • matt and Hot Licks tnx for your responses. My code does compile. The question arise form curiosity "if its possible? ". Regarding the convention of not using import in .h I provided a link (7 comment). – Mike.R Apr 05 '14 at 20:00
  • 2
    @Mike.R - If the only reference to a class is to declare a pointer variable or a pointer parameter, it is (very slightly) better to just do `@class` rather than #import. It saves a miniscule amount of compile time and, in very complex scenarios, can avoid include ordering problems. But as a general strategy it's not worth it. And you can't avoid including the .h if you are declaring an actual variable of the class type (vs a pointer), referencing a method of the class, or subclassing the class. This holds for both C++ and Objective-C. – Hot Licks Apr 05 '14 at 21:43
  • Real purpose of `@class` is just to perform forward declaration, e.g. when both a protocol and a class are being declared in the same header and they both mention the other - that would be impossible without either a `@protocol` or a `@class` beforehand. – matt Apr 06 '14 at 15:30
2

You have to import the superclass, otherwise your subclass has no reference of what to build off. In your subclass.h you should #import "BaseClass.h". You should not have issues with cyclical inclusion because the #import uses header guards to solve this problem.

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
1

There is one file in xcode project which is known as .pch file. In this file you can import the header files. After this no need to import in the other header files as well. But make sure the file which you import is being used in all the files.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56