How to inherit from class in interface? - ios

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.

//#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.

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.

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.

Related

Correct way to include multiple header in Objective C

So far I have been importing the required .h files in the .h files of class where it is required.
I had read yesterday that this is the incorrect way to do it and I should forward declare the class in .h and then import it in .m However, I am confused what is the correct way to do this when it comes to large projects?
Since I will have a lot of imports in lots of files, this way it will make the code long in my opinion. Someone suggested me to create a BaseViewController class which subclasses UIViewController and add all my imports there, and all the UIViewController's that I will create will subclass BaseViewController and not UIViewController directly. This works, however I just want to know the correct way to do it.
Thanks in advance.
你可以这样做.为每一个模块创建一个.h文件,把需要用到的类导入到里面,需要引用时直接引入这个模块的.h文件使用就好.这样不用写过多的.h文件,也避免了pch的导入文件过多的情况.
you can do like this. Creat one .h fiel for everyModuel,and import you want .h file to this .h . And import this .h file to where if you want . But if something .h file is base,please import they to .pch file.
In fact, I find it a little too rampant. But, hopefully useful to you.
Simplest way to import header files in project is to create .pch file.
Create .pch and add classes header in .pch file which you want to import most frequently. Then there is no need to import those headers any of the .h and .m. They will available for them automatically.
Please refer this for how to create .pch file.
In Xcode, when you create a class, by default it will make .h and .m files of that class.
These two files are used to separate between the public and private parts of the class.
The .h file is a header file for public declarations of your class like an API, while the .m file is the private implementation.
You have to import somefile.h in somefile.m
Standard best practice for Objective-C is to forward declare classes in your .h header files and #import them in your .m implementation files. This reduces compile times and forces you to move dependencies to where they are actually required. The exception are base classes and protocols that your class inherits from. These need #imports in the header.
The Google Style Guide for Objective-C specifies a recommended order in which headers should be included in a .m implementation file. This is partly to stop headers from depending on other headers being included before them. The order goes:
Related header (e.g. MyClass.h)
Operating system headers (e.g. Foundation/Foundation.h)
Language library headers (e.g. stdlib.h)
Groups of headers for other dependencies
In general, many #imports and/or forward declarations making your code too long may be an indication that you should refactor to reduce dependencies between classes. Ideally the number of dependencies should be low, though ultimately this depends on the situation.

Is this a good way to structure header importing

I am building an app, where I have made a Menu class which is a tableview being presented to the user (containing link to different view controllers, which should be presented on click).
Instead of importing any viewc. header in either of my viewc.'s I decided to import all the view controllers headers into my Menu.h. This way I can make all app navigation from menu class. I will then import menu.h in my appDelegate.h and then import only appDelegate.h to all my view controllers. Are there any unforeseen downsides to this, or should I do it another way? Thanks
If it compiles then that's OK, however it's considered best to use forward declarations in header files where ever possible to improve compile time.
Any custom type can be forward declared in the header file, of the using class, with:
#class YourViewController;
and then within the implementation file of the using class, include the actual header file:
#import "YourViewController.h"
Using forward declaration can also stop dependency loops where A.h includes B.h, which itself includes A.h.
Doing this you will have a include loop (which is bad !).
Menu.h is importing ViewController1.h
AppDelegate.h is importing Menu.h (and ViewController1.h)
ViewController1.h is importing AppDelegate.h (and Menu.h (and ViewController1.h))
A clean solution is to use Forward declaration when needed, and doing your import in .m files

Subclassing UIGestureRecognizer with Swift and Xcode 6 - how do I import UIGestureRecognizerSubclass.h?

You can't write to self.state in the subclass unless you import UIGestureRecognizerSubclass.h as indicated here.
In a Swift environment, I'm confused how I'd go about importing this. I tried import UIGestureRecognizerSubclass.h, and without the .h, but I still can't write to self.state.
How would I accomplish this?
The Swift equivalent is simply:
import UIKit.UIGestureRecognizerSubclass
That imports the appropriate header.
You need to have or create a -Bridging-Header.h file to import objc headers such as the one you want. The import line looks like this:
#import <UIKit/UIGestureRecognizerSubclass.h>
If you don't already have a bridge header file in your app, the easiest way to get one is to add an objc class to your project, and xcode will ask if you want one, then creates the file and ties it into the settings for you. You can then delete the objc class.
Everything in that header file is automatically made available to your Swift code, no need to add any import lines in your swift files.

Swift compiling bug "Unknown type name" and "Expected a type"

I'm running into an issue where my Swift code compiles fine but then the generated -Swift.h file has an error in it...
Here is an example of what I'm doing:
class MyScene : CCLayer {
var ctrl : CCControlSlider?
}
This compiles just fine in the swift code and I can see the methods on the ctrl object just fine as well. I have the following in my bridge header:
#import "cocos2d.h"
#import "CCControlSlider.h"
This works perfectly fine in other classes that use other libraries which work correctly. Also note that I can use this CCControlSlider class in my objective-c classes with no issues at all as well.
Here is what happens on the generated -Swift.h file:
SWIFT_CLASS("_TtC15MyProject10MyScene")
#interface MyScene : CCLayer
#property (nonatomic) CCControlSlider * ctrl;
#end
The property has the error "Unknown type name "CCControlSlider" and if it's used in a method then it gives the error "Expected a type".
This works just fine using other classes but for some reason this one class gives this compiler error only in the generated header file and only when used from Swift.
I guess what I'm wondering is, am I doing something wrong or is this just a bug??
While incorporating a set of Objective C files into my mostly Swift-based project I suddenly started getting a lot of these errors on them. Then I realized though the file had:
#import <Foundation/Foundation.h>
It did not have:
#import <UIKit/UIKit.h>
and once I added that line the errors started resolving. Cleaning afterwards always helps. Good luck
NOTE: With newer versions of Swift imports are much easier to type and are done like so:
import UIKit
If you don't want to worry about the order of imports everywhere you import your ProjectName-Swift.h file, try this:
Create a file named ProjectName-Swift-Fixed.h with the following contents
// ProjectName-Swift-Fixed.h
#class CCControlSlider;
// (Add other imports or forward declarations here)
#import "ProjectName-Swift.h"
Second, throughout the rest of your codebase, replace #import "ProjectName-Swift.h" with #import "ProjectName-Swift-Fixed.h"
This approach has two benefits over copying and pasting a list of #import statements above each place you use Swift classes in Objective-C code:
You won't have to worry about the order of imports, except in the ProjectName-Swift-Fixed.h file
When you add new Objective-C classes to your Swift code, you won't have to add new #import statements in every Objective-C file that uses Swift code
The answer given here is the simplest approach: https://stackoverflow.com/a/24216718/341994
Basically, somewhere in your Objective-C code you are importing the automatically generated -Swift.h header. In that same code, before that #import line, insert #import "CCControlSlider.h". The order of these two #import statements is crucial!
The fact that this Objective-C class may not need CCControlSlider is irrelevant (though if it does, that's a bonus). The important thing is the order. We want to expose the namespace to CCControlSlider before exposing the namespace to the automatically generated -Swift.h header.
OC code has a global call # import "projectName - swift. H", and "the projectName - Bridging - Header. H" and calls the OC code.Is equivalent to the parent class call subclasses, subclasses and calls the superclass.To make the "projectName - Bridging - Header. H" calls the oc class don't call # import "projectName - swift. H.

Import useless headers

I have a big project which is refactored a lot.
Some classes import unnecessary headers.
#import "someClass1.h"
#import "someClass2.h"
#import "someClass3.h"
f.e:
classA in interface has:
#import "classB.h"
#import "classC.h"
classB in interface has:
#import "classC.h"
So that import classC in classA interface could be removed...
I would like to have my project clean and tidy.
I'm just wondering:
Is there a way to find useless imports quickly?
What is best way for such situations and organizing one interface import of other interfaces?
Does it affect on application performance if you have plenty of useless/duplicated imports?
Any help appreciated
1) No there is no a way you need to check if that import is used in your file (use find function for this);
2) The best way is import UNTIL IS POSSIBLE header in implementation file (.m). If you need to declare that class in your header file (.h) you can use:
#class nameClass
before begin with #interface...etc.
If you already know that instead you need to import that class header file in your header, you can do it. Be careful to avoid #import cycle. In fact suppose to have this situation:
MGCustomViewController.h:
#import "MGViewController.h"
#interface MGCustomViewController : UIViewController
#end
MGViewController.h:
#import "MGCustomViewController.h"
#interface MGViewController : MGCustomViewController
#end
This cause an error at compile time, because you inherit from MGCustomViewController but importing MGCustomViewController.h you are importing again this last header before that the compiler read the #interface MGCustomViewController directive and so will show an error that says something like SUPERCLASS NOT FOUND.
3) No, but effects on the compile time.

Resources