Use classes without import header - objective-c

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

Related

Circular Importing of ViewController

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).

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"

Strange Delegate Protocol Declaration error?

So in a header file I have the following code:
#import <AudioToolbox/AudioToolbox.h>
#interface alertController:UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer *player;
}
I have both the framework linked and It works just fine in another file within the same project, but I am getting the error that it cannot find the protocol declaration for "AVAudioPlayerDelegate", nor does it recognize the line AVAudioPlayer * audioPlayer because it is an unknown type name. Any ideas why this isn't working despite my import statement, including the framework, and the fact that it's working fine in another file?
AVAudioPlayer (and its delegate) come from AVFoundation, not AudioToolbox. Your other project probably imports <AVFoundation/AVFoundation.h> either through a separate included header file or through your PCH. You should replace that #import with
#import <AVFoundation/AVFoundation.h>

Declaring a subclass in ObjC fails

Here's what I'm trying to do:
I want to put away some methods into separate .h and .m files for a better overview of my code.
So basically I have the myViewController which I want to extend with the method myReactionOnAnimationDidEnd: as a category.
So I declared in "myCustomClasses.h" the following to extend it with my desired method:
#import "myViewController.h"
#interface myViewController (myReactionOnAnimationDidEnd)
- (void)myReactionOnAnimationDidEnd:(NSNotification *)aNotification;
#end
The implementation in "myCustomClasses.m" is:
#import "myCustomClasses.h"
#import "myViewController.h"
#implementation myViewController (myReactionOnAnimationDidEnd)
- (void)myReactionOnAnimationDidEnd:(NSNotification *)aNotification {
self.myLabel1.text = #"Test";
}
#end
The Compiler throws a build failed error "Cannot find interface declaration for 'myViewController'"
So here's my questions:
The first weird thing is, that everything works fine if I do exactly the same for UIViewController instead of myViewController. But since myViewController is a subclass of UIViewController, why shouldn't it work for myViewController as well(#interface iSnahViewController : UIViewController)?
The other weird thing is that the #implementation in "myCustomClasses.m" works just fine if I skip on the #declaration completely. Now how can that be??
Thank you guys!
Any help much appreciated!!
Hans
Now, the funny thing is, that this very same building error comes up even if I create the category with the New File -> ObjC - Category Template. It basically creates the following two files:
in the header file:
#import "myViewController.h"
#interface myViewController (myCategories) //<-- "Cannot find interface declaration for 'myViewController'"
#end
and with the .m file
#import "myViewController+myCategories.h"
#implementation myViewController (myCategories)
#end
And that's already enough to bring up the error from above.
myCustomClasses.m should probably include myCustomClasses.h and not myViewController.h, so the compiler sees the category declaration. Since myCustomClasses.h already includes myViewController.h, the compiler should have all necessary declarations.
I finally got it working!
As the building error occured, I was importing the Category-.h File into the primery Class's .h file:
#import "myViewController+myCategories.h" //<-- in the "myViewController.h"
which lead to the building error from above. This seems to be wrong! So don't do that.
I still don't fully understand how the "myViewController" Class gets to know about it's categories without even having their .h files imported, but as this appears to be a working way of how this is done, I wanted to share with you.
Thanks everybody for helping!!

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.]