I'm trying to test my core data scheme. However, it seems I am unable to create the context because it says No visible #interface for 'MyAppDelegate' declares the selector 'managedObjectContext'.
In online tutorials this method seems to be auto-generated when we create the app. However, in my case it doesn't exist.
This is MyAppDelegate:
Header
#import <UIKit/UIKit.h>
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m file
#import "MyAppDelegate.h"
#implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
// Override point for customization after application launch.
return YES;
}
How should I fix this in Xcode 5 with iOS 7?
I think the best way for you is to create a Master-Detail Application with Xcode 5 and don't forget to check Use Core Data :
With that, you will have an AppDelegate.h and an AppDelegate.m configured with a managedObjectContext.
You will have a project configured correctly with Core Data and a .xcdatamodeld to use easily your SQLite database.
Related
I would like to pass a string to all my other view controllers from the AppDelegate. I have defined a variable called appName and assign it a string
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSString *appName;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize appName;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
appName=#"SatKacPara";
return YES;
}
The following implementation does not have access to appName property. I could not able to figure out.
ViewController1.m
appLabel.text=[(AppDelegate*) [UIApplication shareApplication].delegate ].appName;
You have a simple typo in your line of code. It should be:
appLabel.text = ((AppDelegate*)[UIApplication sharedApplication].delegate).appName;
But using such a property isn't necessary. You can get the app's display name without hardcoding it.
Get rid of your appName property and change this line of code to:
appLabel.text = [NSBundle mainBundle].infoDictionary[#"CFBundleDisplayName"];
This has the advantage of showing the right name even if you rename your app or localize it.
I am using Xcode 5
This is my AppDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
#end
This is my AppDelegate.m file
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self.viewController saveData];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[self.viewController loadData];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
This is my ViewController.h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
//some variables declared here
#interface ViewController : UIViewController <UIActionSheetDelegate, UINavigationBarDelegate, UIImagePickerControllerDelegate>{
//some outlets declared here
}
//some actions and - (void) declared here
- (NSString *) getFilePath;
- (void) saveData;
- (void) loadData;
#end
Beside the code [self.viewController saveData] and [self.viewController loadData] there's an error message:'No visible #interface for 'UIViewController' declares the selector 'saveData' and 'No visible #interface for 'UIViewController' declares the selector 'loadData'.
What should I do?
You should specify class of UIViewController in your AppDelegate.h file:
#property (strong, nonatomic) ViewController *viewController;
and add import statement in this file
#import "ViewController.h"
Your viewController is subclass of UIViewController class so there is not saveData/LoadData method.
The two method exists in your custom class - ViewController.
The solution is replace line in AppDelegate.h from:
#property (strong, nonatomic) UIViewController *viewController;
to:
#property (strong, nonatomic) ViewController *viewController;
or cast viewController to ViewController every time you call saveData/loadData
If the view controller in the app delegate is truly a ViewController object, then change the property to say so:
#property (strong, nonatomic) ViewController *viewController;
You will then also need the matching #class (.h) and #import (.m) statements.
I'm now building up Core Data based iOS application and when I tried to insert new managed object by executing [NSEntityDescription insertNewObjectForEintityForName:#"myModel" inManagedObjectContext:_managedObjectContext]; within AppDelegate.m, I got the error described on the title.
Here's my AppDelegate.h file:
#import
#interface AppDelegate : UIResponder
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
// maybe required?
//#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#end
And here's my AppDelegate.m file (only displaying the relevant part):
#import "AppDelegate.h"
#import "MyModel.h"
#import "listViewController.h"
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//managed object context settings
UITabBarController *tabbarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [[tabbarController viewControllers] objectAtIndex:0];
listViewController *listcontroller = [[navigationController viewControllers] objectAtIndex:0];
listcontroller.managedObjectContext = self.managedObjectContext;
NSLog(#"%#", _persistentStoreCoordinator.managedObjectModel);
MyModel *newMyModel = [NSEntityDescription insertNewObjectForEntityForName:#"MyModel" inManagedObjectContext:_managedObjectContext];
return YES;
}
in this application, I want to use tabbar controller as root view controller and when the app starts, I want to navigation controller as the tabbar controller's root view controller, and use tableview controller as the navigation controller's root view controller. And in the table view controller, I want to use Core Data functionality to display a lot of entities to users.
If I used breakpoint at the exact point of the NSLog() output, it didn't return any errors. And when I moved one line forward to output the log message, the following output returns:
() isEditable 0, entities {
}, fetch request templates {
}
which means I don't have entities for some reasons.
So why are there no entities in this situation? From this answer here over SO, I don't misspelled my entity name. Also, my objectModelContext is not set to nil. So did I set the wrong managed object? Am I doing something wrong in the first three lines in didFinishLaunchingWithOptions method?
Or is there something that is causing the issue here? Or what am I missing?
I use iOS 7 and Xcode 5 and I don't have any managed objects in my entity - after all, the error happened when I tried to instantiate those managed objects.
Thanks.
As discussed in the comments above, it looks like you probably needed to check how your ManagedObjectModel was being initialized.
I'd personally recommend avoiding placing Core Data code directly in your app delegate - I don't think Apple's templates do this very well. Check out this blog post as a great example of the minimal amount of code that's required to set up a Core Data stack, as well as a brief explanation of what each part does.
Regarding your extra comment question - the managedObjectContext method you have is called whenever your managedObjectContext property is accessed. So, when you do:
listcontroller.managedObjectContext = self.managedObjectContext;
This calls the managedObjectContext method on self, which (if I remember correctly) will initialize your context.
I tried to make Renren iOS app along with SocialPlugin docs.
http://wiki.mobile.renren.com/en/index.php/Social_Plugin_Download
I made AppDelegate belows. But every time I invoke app, it failed
at the point of RMConnectCenter initializeConnectWithAPIKey.
The Code is like this.
--noriakiAppDelegate.h--
#import <UIKit/UIKit.h>
#import "RMConnectCenter.h"
#class noriakiViewController;
#interface noriakiAppDelegate : UIResponder <UIApplicationDelegate,RenrenMobileDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) noriakiViewController *noriakiviewcontroller;
#end
--noriakiAppDelegate.h.m--
#import "noriakiAppDelegate.h"
#import "noriakiViewController.h"
#implementation noriakiAppDelegate
#synthesize window = _window;
#synthesize noriakiviewcontroller = _noriakiviewcontroller;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[RMConnectCenter initializeConnectWithAPIKey:#"MY API KEY" secretKey:#"MY SECRET" appId:#"2080970" mobileDelegate:self];
return YES;
}
You mean application crashes when you call initializeConnectWithAPIKey ?
You are not passing your API Key and secret
[RMConnectCenter initializeConnectWithAPIKey:#"MY API KEY" secretKey:#"MY SECRET" appId:#"2080970" mobileDelegate:self];
Are you using storyboards?
I imported the AppDelegate.h in a lot of classes with:
#import "AppDelegate.h"
#interface LoginViewController : UIViewController<UITextFieldDelegate>
{
}
#property (nonatomic, retain) AppDelegate *app;
but somehow it stopped working in my loginviewcontroller.h. It says:
unknown type name 'AppDelegate' [1]
property with 'retain (or strong)' attribute must be of object type [3]
I made this class at the start and it always worked like it should. I didn't make any changes to the class or the AppDelegate when it starting with this error.
I can import it in other classes without problems. I also tried to recreate the class but it didn't help.
Anyone got any idea how to solve this weird error?
It's not a good idea to use this line of code
#property (nonatomic, retain) AppDelegate *app;
in every class you need it. The simple way to access the delegate app where you need it is to do the following:
AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
obviously you need to do:
#import "AppDelegate.h"
in the class where you use it.
If you want a cleaner way to do this, you can create a class method in your AppDelegate.h like the following:
+(AppDelegate*)sharedAppdelegate;
in AppDelegate.m is defined as follow:
+(AppDelegate*)sharedAppdelegate
{
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
}
Then, where you need it you could just call (after importing AppDelegate.h):
AppDelegate* sharedApp = [AppDelegate sharedAppdelegate];
Hope it helps.
P.S. Why do you need to access the delegate?
Declare a forward reference in .h file
#class AppDelegate
#interface LoginViewController : UIViewController<UITextFieldDelegate>
{
}
// keep it as assign rather than retain to keep retainCount leveled for the variable
#property (nonatomic, assign) AppDelegate *app;
in .m file, grab the pointer to Appdelegate by importing AppDelegate.h and then assigning the variable
#import "AppDelegate.h"
- (void)viewDidLoad
{
self.app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//use the variable.
}