Strange Delegate Protocol Declaration error? - objective-c

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>

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

How to prevent circular reference when Swift bridging header imports a file that imports Hopscotch-Swift.h itself

I am integrating Swift into a large existing Objective C project and have run into what I think is a circular reference.
The classes in question are as follows:
Objective C Controller
#import "Hopscotch-Swift.h"
#interface MyController : UIViewController<MyProtocol>
...
#end
Swift Protocol
#objc protocol MyProtocol: NSObjectProtocol {
...
}
Bridging Header
#import "MyController.h"
This code fails to compile because the Hopscotch-Swift.h file will not generate.
I think this is due to a circular reference error as I can import Hopscotch-Swift.h into objective c headers that are not included in Hopscotch-Bridging-Header.h and it works fine.
Is there a workaround for this issue or should I file a radar with Apple?
Forward declaration should work, in your case.
In your .h:
#protocol MyProtocol;
#interface MyController : UIViewController<MyProtocol>
#end
In your .m:
#import "HopScotch-Swift.h"
From How can I add forward class references used in the -Swift.h header? and the Swift interoperability guide:
If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
I ran into this when trying to use Swift classes inside Objective-C protocols, where the protocol was also implemented by another Swift class. It reeked of circular references and I guessed that it might be a problem trying to circularly generate the bridging headers, rather than a 'normal' circular include problem.
The solution, for me, was to just use forward declarations before the protocol declaration:-
// don't include the MyProject-Swift.h header
// forward declaration of Swift classes used
#class SwiftClass;
#protocol MyProtocol <NSObject>
- (SwiftClass *)swiftClass;
#end
The forward declaration by itself didn't work for me. It compiled without errors but still had warnings that the protocol couldn't be found. I treat all warnings as errors, so this isn't good enough.
I was able to fix it by moving the protocol implementation into another category header.
So here's what worked for me:
In my MyOtherSwiftFile.swift:
#objc protocol MyProtocol: class {
func viewController(didFinishEditing viewController: MyViewController)
}
In my MyViewController.h:
#interface MyViewController // Removed protocol implementation declaration here
#end
Added MyViewController+MyProtocol.h to project, and put this in there:
#interface MyViewController (MyProtocol) <MyProtocol>
#end
The methods themselves can stay where they are if you want.
After you implement the above and compile, you'll get compiler warning(s) somewhere in your code that requires that MyViewController implements MyProtocol. In that file, you will #import "MyViewController+MyProtocol.h"
Alternatively you can convert your protocol to an Objective-C protocol MyProtocol.h and then use it in Swift by including MyProtocol.h in your bridging header.
You could something like this in the .h file you suspect to trigger the circular reference:
#ifndef MY_HEADER_H
#define MY_HEADER_H
your header file
#endif

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"

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

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.