I have an iPhone app with a custom class, Detail, a subclass of UIViewController that I created.
I need to make a subclass of Detail, and I want to to call it ActivityDetail. So I wrote the following in my ActivityDetail.h file:
#import <UIKit/UIKit.h>
#import "Detail.h"
#interface ActivityDetail : Detail {
}
#end
The problem is that I'm getting a compiler error telling me this:
error: cannot find interface
declaration for 'Detail', superclass
of 'ActivityDetail'
And the strange thing is: I can change the superclass from Detail to UIView (for example), compile getting many errors (obviously), and then change the superclass to Detail again and everything works fine! But if I then change anything to the Detail class the problem comes back from the beginning...
How can I solve this?
It is recommended to not import classes beyond the default Foundation or UIKit imports in your header files. Instead you should do something similar:
Header
#import <UIKit/UIKit.h>
#class Detail;
#interface ActivityDetail : Detail {
}
#end
Implementation
#import "ActivityDetail.h"
#import "Detail.h"
#implementation ActivityDetail
#end
This allows your header to "know" about additional classes without forcing all "importers" of that header to also import everything it imports.
Here is a great reference question, and a great answer, regarding the usage of #class and #import.
Erm you are importing Detail as Dettaglio.h. Probably the compiler is not fluent in Italian.
Are you importing the .h where Detail is declared?
#import "Detail.h"
Either #import "Detail.h" or subclass from Dettaglio (depends on whichever one your Detail class is actually named.
Related
I have a swift project which I'm making use of MBProgressHUD in through a Bridging header file. The issue I'm having is that UIView doesn't appear to be recognised as type and I don't know why.
In my bridging header I have:
#import "MBProgressHUD.h"
The errors I get when I try to build are all along the same lines:
Cannot find interface declaration for 'UIView', superclass of MBProgressHUD.
I have checked the MBProgressHUD file and I can see that it definitely imports the following:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import "MBProgressHUD.h"
#import "CSNotificationView.h"
Has anyone else seen a similar issue? If so do you know what the issue is and how can I fix it?
I also come across the same issue and thats what i did to use MBProgressHud with swift 2
1) Specify use_frameworks! in your Podfile to use frameworks.
2) Add #import in your bridging header, use angle brackets instead of double quotes e.g -
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import <MBProgressHUD/MBProgressHUD.h>
3) in your swift file also import MBProgressHUD e.g.
import UIKit
import MBProgressHUD
Now you can use MBProgressHud like -
MBProgressHUD.showHUDAddedTo(self.view, animated: true);
Hope it will help.
Remove your existing bridging header file and add a new one.
Make sure you are adding your bridging header path in SWIFT_OBJC_BRIDGING_HEADER under the target section instead of the project section.
You could also try adding a precompiled prefix header file (.pch) to your project. You'll find it under File/New/Other, there add the #import <UIKit/UIKit.h>clause, and then in the target's build settings, under Apple LLVM 7.0 - Language, set the Precompile Prefix Header flag to yes and add the .pch file like this "YourProjectName/YourProject-Prefix.pch".
See also this answer.
If you include the MBProgressHUD library with CocoaPods try to include a line similar to this
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import "MBProgressHUD.h"
in
BridgeHeader.h and in Objective-C Bridging Header key in Build Settings choose the header.
For test if the library is correctly added I try to show a progress with this instruction in the ViewController:
MBProgressHUD.showHUDAddedTo(self.view, animated: true);
I tried it in a new project and it works.
I'm brand new to iPhone programming. I've tried some tutorials and such and now I'm trying to do a simple project to get my feet wet. And of course I have having issues getting it to do what I want. I started it by using a Navigation project. I didn't want the UITableView though I just want normal buttons that lead to the next page. Although when I tried to get rid of the UITableView I think I did it wrong so now it isn't working when I am setting up the next view.
Here is the .h (the red underline is under the MPHViewController in both places) Oh and what it wants to do is make both those UIViewController.
#import <UIKit/UIKit.h>
#import "MPHViewController.h"
#interface RootViewController : UIViewController {
MPHViewController *mphViewController;
}
#property(nonatomic,retain) MPHViewController *mphViewController;
#end
I'm thinking where I mainly messed up was in the .m (I think) I took out all the code that had to do with the UITableView and simply put this in (wasn't sure what else should be placed there).
- (UIViewController *)viewControl:(UIView *)view
{
}
Sorry for being a pain I just don't know any better way of learning how to do this other then just trial and error.
It might not be able to find the definition for your custom class.
In xcode under the project setting -> build phases, make sure the MPHViewController.m is included in the list of source files to be compiled.
Also, make sure in your MPHViewController.h that your subclassing UIViewController properly:
#interface MPHViewController : UIViewController
{
// ....
}
Make sure you synthesize it in the .m
#synthesize mphViewController;
I've got a delegate setup (of course) and I'm trying to reference it directly in one of my controller, but if I include the header file for my delegate (which works already) I get an error:
Expected specifier-qualifier-list before 'RootViewController' in my delegate header.
Here are the lines of code that error (in SurveyClientAppDelegate.h):
#interface SurveyClientAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
So, to clarify, everything works until I use this in one of my controllers:
#import "SurveyClientAppDelegate.h"
If anyone needs more of my code to help me, I'd be more than happy to post.
I'd just hate to have an overwhelming question with tons of excess code and not get an answer.
Does you app delegate import RootViewController? Does it have this in the header?
#import "RootViewController.h"
If so, get rid of it and add #class RootViewController; in it's place.
EDIT: Then place the import in the .m if it's not currently there.
Please explain your answers as I've gotten away with not having to do this so far.
Thanks
#class is the same as a class forward declaration in C++.
You could simply #import the header file of each view controller, but it's sometimes cleaner to do forward declarations (#class) in the .h file, and only do the #import in your AppDelegate's .m file.
You only import things you need, and #class is doing something similar to #import - it's letting the compiler know a type exists. So why would you tell the app delegate about a class it will never see?
The difference is:
#class will only say the class exists, nothing more.
#import tells the code what messages the class accepts or anything else the header file declares. So you use it when the code needs to actually send messages to an object.
That's why a very general pattern is to use #class in the header, and #import in the implementation file. Sometimes you do need to import in the header, again if you have to know anything more than "this class exists".
Short answer: No. See this question: #class vs. #import
1) I have the CoreData.framework imported. In Groups & Files I see it in the Framworks list together with UIKit.framework, Foundation.framework, CoreGraphics.framework.
2) I have this code, which should actually work. Don't know what that error means...
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface RootViewController : UITableViewController <CLLocationManagerDelegate> {
NSMutableArray *dataArray;
NSManagedObjectContext *managedObjectContext; // HERE's THE ERROR LINE
}
Edit: After importing CoreData, the error disappeared. BUT: Actually UIKit contains it, or not??
#import <CoreData/CoreData.h>
I have an Apple example code and they NEVER import CoreData, and it works.
Apple is taking advantage of the precompiled header (.pch file). Your projects do too when you start with a template and check the "Use Core Data for storage" option.
Also, you can use the precompiled header file to add any header files you wish to import for all source files in your target and project.
You need to link the CoreData framework and import CoreData/CoreData.h in the header file. UIKit does not contain the Core Data framework.