Circular Importing of ViewController - objective-c

In Appdelegate I am Importing Some Class for showing the view and in that view I have required to import the Same AppDelegate for accessing some method and variable but when I am doing this. Its shows circular import error.
I have tried by importing by #class in .h file and import in .m file.

In both classes you should only use #class in the .h file and then add the #import in the .m file. This is the correct solution for the problem you describe.
Note that the circularity could be bigger because a 3rd class could be #imported and it in turn #imports another class to make the circular link. Generally you should use #class in your .h files unless you have to #import (for the superclass and #protocols you implement).

Related

Use classes without import header

I have some weird codes.
ClassA.h
// there is no import code
#protocol Album <Meta>
#end
#interface MyAlbum: VariantMeta <Album>
#end
What I know is this.
If I want to use any classes which are declared from outside of current file (in this case Meta and VariantMeta) I have to import those classes header files.
But Class.h can use Meta without imports any headers.
There should be #import 'Meta.h'; in my understanding.
But there is not!
Can I get any hints for understanding this situation?
help me
thanks
Update
Class.m file looks like this.
// Class.m
#import <Framework/SomeHeaderWhichImportMetaHeaderFile>
#import "ClassA.h"
#implementation MyAlbum
#end
I guess this Class.m file's import, somehow, affects header file. but I don't know how it can be...

Why is Core Plot delegate protocol not found?

I try to use Rays tutorial of Core plot for my iPad App
I use a SplitViewController
And I want to have a scatter plot in my DetailViewController
I change my Code to:
#interface GWDetailViewController : UIViewController <UISplitViewControllerDelegate,CPTPlotDataSource>
and now I get this error:
Cannot find protocol declaration for 'CPTPlotDataSource'
to add frameworks and librarys was no problem
What is wrong?
Make sure that the .h file for your view controller contains the following import statement:
#import "CorePlot-CocoaTouch.h"
If you dont want to import header file every time, you can Import it in .pch file for once.
In your scenario ,Open .pch file(usually placed in supporting file folder ) and add following code.
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"
#import "CPDConstants.h"
#import "CPDStockPriceStore.h"

How can i use MBProgressHUD in multiple ViewControllers?

I have a problem using the MBProgressHUD library. I can use it normally in my main ViewController, but, then, when i try to import the .h file in other view controller, i have the following error:
Redefinition of enumerator 'MBProgressHUDModeIndeterminate'
Redefinition of enumerator 'MBProgressHUDModeDeterminate'
...
Does anyone knows how can i manage it?
Thanks
EDIT: I have the following imports in my main view controller
#import "ViewController.h"
#import "FMDatabase.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#implementation ViewController
When i try to put this imports in other view controller, i get the errors:
#import "FinalViewController.h"
#import "Parse/Parse.h"
#import "MBProgressHUD.h"
#implementation FinalViewController
I have used the MBProgressHUD library from this link https://github.com/matej/MBProgressHUD
You're probably #include-ing MBProgressHUD.h rather that #import-ing it. #import ensures that it's only imported once.
I've used MBProgressHUD exclusively in my apps, and across multiple view controllers and never seen that issue before.
You could try and add your import statement to your .pch file rather than import for each view controller. It will add the header even if you don't use it, but might be worth a go.
Also, make sure you only have one copy of the header file in your project. The error you are showing almost looks like it is being defined twice which would indicate too many headers.

what does #class do in iOS 4 development?

Is there any difference in doing
#class MyViewController;
rather than doing the normal import of the .h into the appdelegate.h
#import "MyViewController.h"
I've seen some example recently that use the #class way and wondered if there any differences.
thanks.
There is a big difference.
#class MyViewController;
Is a forward declaration for the object MyViewController. It is used when you just need to tell the compiler about an object type but have no need to include the header file.
If however you need to create an object of this type and invoke methods on it, you will need to:
#import "MyViewController.h"
But normally this is done in the .m file.
An additional use of forward declarations is when you define a #protocol in the same header file as an object that uses it.
#protocol MyProtocolDelegate; //forward declaration
#interface MyObject {
id<MyProtocolDelegate> delegate;
...
}
...
#end
#protocol MyProtocolDelegate
... //protocol definition
#end
In the above example the compiler needs to know that the #protocol MyProtocolDelegate is valid before it can compile the MyObject object.
Simply moving the protocol definition above MyObject definition would also work.
#class allows you to declare that a symbol is an Objective-c class name without the need to #import the header file that defines the class.
You would use this where you only need the class name defined for the purposes of declaring a pointer to the class or a method parameter of the class, and you do not need to access any methods, fields, or properties in the class.
It saves a minuscule amount of compile time vs the #import, and it sometimes helps avoid messy include circularity issues.
[And, as rjstelling points out, it's sometimes useful where you have interleaved declarations and you need to "forward declare" something.]

Xcode: Get rid of forward class warning

In Xcode, I have a forward class declared so as to avoid a circular import e.g.
#class MyClass;
And then I do a method call on that class e.g.
[MyClass myMethod];
But I get a forward class warning e.g.
warning: receiver 'MyClass' is a
forward class and corresponding
#interface may not exist
How can I hide those across my whole project?
You use forward class declarations in your header file to prevent circular imports.
You must still import the MyClass header in your .m file. The circular import problem doesn't exist with .m files.
My answer to a similar question may be of use here.
The basic concept is this:
use #class in header files, and then use #import in the .m files.