Question about #property and #synthesize - ios4

Usually we have codes like these
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#synthesize window;
And also second version
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *_window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#synthesize window = _window;
Quetions are
what is the big difference ? Which better ? and Why ?
For first version, Why what is the default attribute member of window ,which is _window in second version. Does that KVC or KVO work inside ?

in the default instance it is the same as
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#synthesize window = window;
which would actually not need an Assignment on the synthesize. and would probably generate an error.
This is the normal way that I do it.
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#synthesize window;
I explicitly declare the Instance Variable
UIWindow *window;
so that I know it is there, (they are generally all right next to each other.)
and I separate them by the ones I retain and the ones I dont. So that I know to release them in my dealloc method

Related

Problems accessing 1:N relationship in entity

https://github.com/lokming/QuestionBank
I have entities: Bank,Section,Subsection,Set,Question.
I am having problems accessing relationship "NSSet Subsection" in entity Section and getting the message "[UITableViewCell thesubsection]: unrecognized selector sent to instance" from this code
in CRSectionVC.m which fills a tableview cell
- (NSArray *)allQuestions
{
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision" ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
}
I can however access the "NNSet section" relationship in Bank entity using this code
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
_section2 is declared in CRSubsectionVC.h
#property (strong, nonatomic) Section *section2;
The storyboard is
1. CRMasterViewController which displays ‘category’ attribute from Bank entity into a tableview,
Bank.h
#class Section;
#interface Bank : NSManagedObject
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSSet *thesection;
#end
Bank.m
#implementation Bank
#dynamic category;
#dynamic thesection;
#end
When I tap a ‘category’ I segue and pass a Bank object to CRDetailViewController. I use following code:
NSSortDescriptor *division2 = [NSSortDescriptor sortDescriptorWithKey:#"division" ascending:YES];
return [self.detailItem2.thesection sortedArrayUsingDescriptors:#[division2]];
to fetch section relationship (NSSet *thesection) ‘division’ attribute from Bank into tableview.
Section.h
#class Bank, Subsection;
#interface Section : NSManagedObject
#property (nonatomic, retain) NSString * division;
#property (nonatomic, retain) Bank *bank;
#property (nonatomic, retain) NSSet *thesubsection;
#end
Section.m
#implementation Section
#dynamic division;
#dynamic bank;
#dynamic thesubsection;
#end
If I tap a ‘section’ ’ I segue and pass a Section object to CRSubsectionVC named _section2. When I try to access NSSet *thesubsection to get ‘subdivision’ attribute using code
NSSortDescriptor *division = [NSSortDescriptor sortDescriptorWithKey:#"subdivision"ascending:YES];
return [_section2.thesubsection sortedArrayUsingDescriptors:#[division]];
I get the error [UITableViewCell thesubsection]: unrecognized selector sent to instance. I cannot figure out why automated accessor ‘thesection’ works OK but not ‘thesubsection’ .
Subsection.h
#class Section, Set;
#interface Subsection : NSManagedObject
#property (nonatomic, retain) NSString * subdivision;
#property (nonatomic, retain) Section *section2;
#property (nonatomic, retain) NSSet *set;
#end
Subsection.m
#implementation Subsection
#dynamic subdivision;
#dynamic section2;
#dynamic set;
#end
Fixed the problem. A case of not copying code exactly from working template and understanding core data and table views.

NSManagedObject Custom Class --NSInvalidArgumentException unrecognized selector --

I have a custom NSManagedObject Class That Looks Like This.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class Ingredient, MenuCategory, Price, ItemSize;
#interface Item : NSManagedObject
#property (nonatomic, retain) NSNumber * collected;
#property (nonatomic, retain) NSString * desc;
#property (nonatomic, retain) NSString * instructions;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * quantity;
#property (nonatomic, retain) NSNumber * selected;
#property (nonatomic, retain) MenuCategory *menuCategory;
#property (nonatomic, retain) NSSet *prices;
#property (nonatomic, retain) NSSet *itemSizes;
#property (nonatomic, retain) NSSet *itemIngredients;
-(NSMutableSet *)mutablePrices;
-(NSMutableSet *)mutableItemIngredients;
#end
#import "Item.h"
#import "Ingredient.h"
#import "MenuCategory.h"
#import "Price.h"
#import "ItemSize.h"
#implementation Item
#dynamic collected;
#dynamic desc;
#dynamic instructions;
#dynamic name;
#dynamic quantity;
#dynamic selected;
#dynamic itemIngredients;
#dynamic menuCategory;
#dynamic prices;
#dynamic itemSizes;
-(NSMutableSet *)mutablePrices{
return [self mutableSetValueForKey:#"prices"];
}
-(NSMutableSet *)mutableItemIngredients{
return [self mutableSetValueForKey:#"itemIngredients"];
}
#end
Nothing so special RIGHT ???
The Following Should Work Right ????
[item.mutablePrices addObject:newPrice]
BUT IT DOES NOT GIVES ME THE FOLLOWING ERROR
2014-03-30 10:25:34.594 restos[1192:60b] -[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
2014-03-30 10:25:34.597 restos[1192:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0'
BUT WHEN I DO THE FOLLOWING
[[item mutableSetValueForKey:#"prices"] addObject:newPrice];
WORKS JUST FINE --- I KNOW IS SOMETHING SIMPLE FOR SOME REASON I CAN NOT SEE IT ----
THANK YOU IN ADVANCE
The error message
-[NSManagedObject mutablePrices]: unrecognized selector sent to instance 0xe8d72c0
indicates that your item object is not an instance of the Item class. A possible reason could be that you did not set the class of the entity to "Item" in the Core Data
model inspector.
Generally it is less error prone if you let Xcode generate the NSManagedObject subclass files (from the Edit menu), and add your custom methods in a Class Category. Alternatively, use "mogenerator".

could not locate an NSManagedObjectModel for entity name "X"

I have this crash error : * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'JourneeDeTravail''
My AppDelegate.h :
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
#end
My AppDelegate.m :
#import "AppDelegate.h"
#import "TableViewController.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize navigationController;
#synthesize managedObjectContext =_managedObjectContext;
#synthesize managedObjectModel =_managedObjectModel;
#synthesize persistentStoreCoordinator=_persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *viewController = [[ViewController alloc] init];
viewController.managedObjectContext = [self managedObjectContext];
NSLog(#"AppDelegate VC: %#", managedObjectContext);
return YES;
}
- (void)dealloc {
[super dealloc];
}
#end
And a ViewController.h :
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#interface ViewController : UIViewController{
NSManagedObjectContext *managedObjectContext;
}
#property (retain, nonatomic) IBOutlet UILabel *displayStart;
#property (retain, nonatomic) IBOutlet UILabel *displayEnd;
#property (retain, nonatomic) IBOutlet UITextField *displayResult;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (IBAction)SaveTest:(id)sender;
#end
And ViewController.m :
#import "ViewController.h"
#implementation ViewController
#synthesize managedObjectContext;
- (void)viewDidLoad {
if (managedObjectContext == nil) {
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
NSLog(#"After managedObjectContext VC: %#", managedObjectContext);
}
}
- (IBAction)SaveTest:(id)sender {
NSLog(#"Dans SaveTest : %#", managedObjectContext);
NSLog(#"Context: %#",managedObjectContext);
NSLog(#"PS Coord : %#",managedObjectContext.persistentStoreCoordinator);
NSLog(#"MOM : %#", managedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(#"Entities : %#", [[managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:#"JourneeDeTravail"]);
JourneeDeTravail *journee = (JourneeDeTravail *)[NSEntityDescription insertNewObjectForEntityForName:#"JourneeDeTravail" inManagedObjectContext:managedObjectContext];
}
But when I press SaveTest button it crashes with error log saying all my log lines are (null).
Of course I have an Entity called JourneeDeTravail...
Any idea ? Looks like I don't have a managedObjectContext but I don't know what to do to fix that. Thanks for your help !
Just for those of you who have the same problem : I found the solution when I did this tutorial : http://www.techotopia.com/index.php/An_iOS_5_iPhone_Core_Data_Tutorial
The debugger says all. It can't find the entity. The entity is in your case the storage. No Core Data, no Memory.
In AppDelegate.m in time of applicationDidFinishLaunching try to instantiate a storage class. Thats the entitiy the ManageObjectModel search for.
like: myMainThreadStorage = [[StorageClass alloc] init];
Or I have completely misunderstood the last 6 hours of studying my problem and solved it with a later [super viewDidLoad]

self.navigationController value gets null in Tabbar VIew

I have a navigation controller as the root view controller. I have a UIView then a UITableView, then again a UITableView, then a TabbarView and each of my tabbarview is a hierarchy of views. Now my problem is that I am able to get the value of self.navigationcontroller till the last UITableView but the value gets null in the TabbarView. Now is there a way I can access the value in the Tabbarview. Do I have to create a new navigation controller for each tabs in the tabbarview and then assign the root navigation controller value to it?
My below code does not work,
The last controller where I can get the value of the navigationController is AssignmentViewController.
Code from AssignmentViewController:
- (void)viewDidLoad {
UITransTabViewController *transtvController = [[UITransTabViewController alloc] initWithNibName:#"TransTabViewController" bundle:nil];
self.transtabViewController = transtvController;
transtvController.naviController=self.navigationController;
[transtvController release];
}
In my UITranTabViewController.h I have the below code,
#interface UITransTabViewController : UIViewController <UITabBarDelegate> {
NSArray *viewControllers;
IBOutlet UITabBar *tabBar;
IBOutlet UITabBarItem *arrivalsTabBarItem;
IBOutlet UITabBarItem *departuresTabBarItem;
UIViewController *selectedViewController;
UINavigationController *naviController;
}
#property (nonatomic, retain) NSArray *viewControllers;
#property (nonatomic, retain) IBOutlet UITabBar *tabBar;
#property (nonatomic, retain) IBOutlet UITabBarItem *arrivalsTabBarItem;
#property (nonatomic, retain) IBOutlet UITabBarItem *departuresTabBarItem;
#property (nonatomic, retain) UIViewController *selectedViewController;
#property (nonatomic, retain) IBOutlet UINavigationController *naviController;
I am trying to set the value of naviController from the AssignmentViewController, but still I get the
NSLog(#"MY NAV CONTROLLER IN TRANSTABVIEWCONTROLLER IS = %#", self.naviController);
value as NULL.

How To Pass ManagedObjectContext To Other View Controllers in Special Case

I have a rootViewController like so:
Header:
#interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *lastUpdateLabel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
-(IBAction)selectHome:(id)sender;
//-(void)loadOverlays;
-(void)testCoreData;
#end
Implementation:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//...
[self testCoreData];
//creating label in tool bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = #"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
//label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
//label.highlighted = YES;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
[lastUpdateLabel initWithCustomView:label];
[label release];
[self.view addSubview:self.navigationController.view];
[self.navigationController.view setFrame:self.view.frame];
}
But I need to transfer my managedObjectModel to my table view and then to a map view so that the map view can make queries depending on what the user wants to see. I was consulting some apple sample code which looks like(from Recipes):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
recipeListController.managedObjectContext = self.managedObjectContext;
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
I know that is in the appDelegate, but I figure that I can do the same when a row is selected or another view is pushed onto the stack, right? The problem is that I have configured most of my view with a nib which looks like so:
Due to this, I am unable to use a similar strategy that Apple employs to transfer a managedObjectModel to an alternate viewController(in this case PermitListViewController) because I don't access PermitListViewController directly when adding a subview. If anyone has any idea of how I would go about getting my managedObjectModel to my PermitListViewController. Please share! Thanks in advance!
EDIT: I am thinking of placing the managedObjectModel in a singleton class. What are your guys' thoughts on that? Good programming practice? Anything I should be aware of? Thanks.
Why not have the NSManagedObjectContext on the app delegate? Then it would be easily accessible from all your view controllers, and as they are UI they execute on the main thread, and can therefore share the same MOC.
I ended up creating a singleton class for the managedObjectModel using this as a reference (scroll down to "Creating a Singleton Instance").

Resources