Unable to build project with CoreData classes - iphone

I am trying to migrate my sandpit code into my main project but for some reason I am getting the following strange error when trying to compile
syntax error before 'NSManagedObjectModel'
At first I thought this was because coredata wasnt in the prefix.pch file but I have added it in there too.
This is the top of AppDelegate where the code is being used (straight out of an Apple example)
#import <UIKit/UIKit.h>
#import "AppSettings.h"
#import "Skin.h"
#interface JeanieAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;

#import <CoreData/CoreData.h> and don't forget to link it in.

Also, beware adding just anything to your .pch file. When you do so, those header files will be included all throughout your projectYou should only really put things there that are truly going to be universally required all through your project.

Related

Objective-C using a class without creating multiple instances [duplicate]

I have been mixing Swift and Objective C just fine but I'm having issues gaining access to a Swift class from the HEADER file of Objective C. I can do so successfully in the .m file.
Within the .h file I import the Xcode generated file that has the format "Appname-Swift.h". However, in doing so I get the message that the file is not found. I can do this same import in my .m file with no issue. However, I need it in the .h file as I reference a Swift class that I need access to with public API.
How can I make use of the Swift class from the .h portion of Objective C?
Example:
#import <UIKit/UIKit.h>
#import "MyApp-Swift.h"
#interface SelectedContactsVC : UIViewController
#property (nonatomic,strong) MapVC *mapVC;
#end
MapVC above is a Swift class.
Move #import "MyApp-Swift.h" to .m file.
And make your .h file as:
#import <UIKit/UIKit.h>
#class MapVC;
#interface SelectedContactsVC : UIViewController
#property (nonatomic,strong) MapVC *mapVC;
#end
Swift cannot generate "MyApp-Swift.h", if it's imported from Objective-C header, sort of mutual dependency thing maybe.

AppDelegate.h errors - XCode

This is really weird. When coming back from school today and firing up my Mac Mini, I open up Xcode and I am presented with two errors in my AppDelegate.h file. One of these errors says Expected selector for Objective-C method and the other one is Expected method body. I googled these two errors and found nothing that could solve my case. I have tried restarting Xcode and as well as my computer. I have also tried "Cleaning" the project and still my problem is not resolved. What could this error be? Is it on my end? Or is this an Xcode bug? Thanks:
Code: AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
Check Your main.m File for any extra or invisible Code:
//
// main.m
// Demo
//
// Created by Stackoverflow on 12/20/12.
//
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
What's in the corresponding .m file?
Are you #synthesizing accessors for your two properties? Failing to provide accessors (either synthesizing them or providing your own) seems like the most likely cause of the errors you've shown.
Have you implemented the usual app delegate methods? Off the top of my head, I'm not sure which if any app delegate methods are actually required, but your app isn't going to work very well and will produce some warnings if you don't have at least a -applicationDidFinishLaunching:withOptions:.
With Xcode newer than 5.0, Anyone confronting this issue can from the top menu\Product\Build.
It should resume right in the spot.

Import directive not giving access to the class

I am including a file like this:
#import "BannerPhoneContentController.h"
however, when I try to use it like this:
bannerContentController = [[BannerPhoneContentController alloc] init];
I get the error:
Use of undeclared identifier 'BannerPhoneContentController';
what could be causing the code to seemingly ignore my import?
this is the header for banner content controller.
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "BannerContentController.h"
#interface BannerPhoneContentController : BannerContentController <UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) UIPageControl *pageControl;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
#end
EDIT bannerContentController is of type BannerContentController NOT BannerPhoneContentController. The latter is a subtype of the former. It is definitely worth noting that this exact code works just fine in another app, so it's nothing to do with the code itself - just how it's being included apparently. I'm stumped.
EDIT ok found the problem - not sure how to fix it. I generated the preprocessed file and the header file it's included is in fact this:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ContentController.h"
#interface PhoneContentController : ContentController <UIScrollViewDelegate>
{
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) UIPageControl *pageControl;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
#end
note the difference - all the 'Banner's are missing. This is what the file used to look like. However, I've checked and the file that's in the project is the one I posted at the top. I tried cleaning the solution and building again. How can I fix this and why has it happened?
EDIT The file that I'm including all this in is part of a code library. I have now created a new project, imported the code library and included all of this in that library once again in exactly the same way. and it works. I have looked over the two projects and the code in question is identical. This must be a project setting?
It looks for me like recursive import. Say, BannerContentController.h imports BannerPhoneContentController.h, and BannerPhoneContentController.h imports BannerContentController.h.
If it is the case, then you should resolve recursive dependencies. E.g. using forward declaration.
does using #class BannerPhoneContentController in .h and #import "BannerPhoneContentController.h" in .m gives the same result ?
After a lot of messing around, and thanks to a comment from Yuras I generated the preprocessed code and realised the problem was that the OLD version of the code was being included.
More useful is WHY. It's just a bug in XCode that happens sometimes when incremental linking is turned on - basically this is where time is saved by only changing the parts of the pre-processed file when the corresponding bits of your code have been altered. Unfortunately, this does not alway work correctly.
The solution, annoyingly, was to delete the files, clean the build, delete the derived data (window / oraniser / projects / project name / delete derived data), restart xcode (possibly also restart your computer, or throw it out of the window and buy a new one) then re-import the files.
XCode can be a real pain...

Error: "Expected specifier-qualifier-list before 'RootViewController'" in delegate header

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.

Why do I get this error "error: expected specifier-qualifier-list before 'NSManagedObjectContext'?

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.