ivar behave in 2 different way, with same usage - ios

i tried 2 different apps to test few things.
the first app its simple
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
NSMutableString *text;
}
#property (nonatomic, retain)NSMutableString *text;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize text = text;
- (void)viewDidLoad
{
[super viewDidLoad];
text = #"FOO";
NSLog(#"%#", text);
self.text = #"FOO2";
NSLog(#"%#", self.text);
NSLog(#"1:%# - 2:%#", text, self.text);
}
this made things seems like synthesizing with same name, makes them the same variable. cause it print this:
2013-09-05 11:20:14.527 testIvar[12965:c07] FOO
2013-09-05 11:20:14.528 testIvar[12965:c07] FOO2
2013-09-05 11:20:14.529 testIvar[12965:c07] 1:FOO2 - 2:FOO2
even if i use %p to print the address memory of text and self.text i get the same address
*my other app test is this *
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSManagedObjectContext *managedObjectContext;
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
#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;
#property ( strong, nonatomic ) UINavigationController *navigationController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "MasterViewController.h"
#implementation AppDelegate
#synthesize managedObjectContext = managedObjectContext;
#synthesize managedObjectModel = managedObjectModel;
#synthesize persistentStoreCoordinator = persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterVC = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
masterVC.MOC = managedObjectContext;
self.navigationController = [[UINavigationController alloc]initWithRootViewController:masterVC];
[self.window setRootViewController:self.navigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
this app won't work if i do
masterVC.MOC = managedObjectContext;
but only work if i do
masterVC.MOC = self.managedObjectContext;
even if i print the mem address of managedObjectContext and self.managedObjectContext
i get 2 different addresses
how is that possible? same thing in 2 different app, behave in 2 different way?!?!?!?!?!?!?!?!?

masterVC.MOC = self.managedObjectContext; only works because the getter method has been overridden in that case.
See carefully and you will find that in your AppDelegate, there is a method
- (NSManagedObjectContext *)managedObjectContext
When you refer the object by self., the overridden getter method is called.

Related

SWRevealViewController Error. Build Fails, but no error

First of all, let me give you a heads-up. I'm moderately familiar with XCode (5) and Objective-C.
But, consider me a noob and please help me out.
I'm trying to build an app. I need a Slide out menu in all Views of the app.
I made a dummy project using SwRevealViewController Class. And I have made two slide out menus also. And it works.
I tried to use the same method in the App that I'm building.
But my build has failed with no Errors and a single warning, which I had in my previous dummy project....
I have already imported SwRevealViewController.h to AppDelegate.m and ViewController.m
I'm trying to use a TableViewController as slideout menu (which is empty, I haven't added any code to it yet). But the build is failing.
Edit:
" The error is as follows..
"#synthesize of 'weak' property is only allowed in ARC or GC Mode"
I'm using Core Data with my project. And I have to use webservice extensively in my App. So I have disabled ARC. "
I have also tried replacing table with other views, but the same result. Now I'm stuck.
I know its some thing with my code, but I couldn't find it.
I'm including code of AppDelegate.m and ViewController.m.
Please check it and tell me what is wrong. I'm at a loss here.
Edit:
" I cannot upload images due to Stack Overflow restrictions. So I have uploaded a screenshot of the error in tinypic. Here is the url.
http://tinypic.com/r/162vli/8 "
Thank you for all your help.
AppDelegate.h
// AppDelegate.h
// IndianBloodBank
//
// Created by Roshith Balendran on 8/5/14.
// Copyright (c) 2014 Olympus. All rights reserved.
//
#import <UIKit/UIKit.h>
#class SWRevealViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *UINV;
#property (strong, nonatomic) SWRevealViewController *splitMenu;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
AppDelegate.m
//
// AppDelegate.m
// IndianBloodBank
//
// Created by Roshith Balendran on 8/5/14.
// Copyright (c) 2014 Olympus. All rights reserved.
//
#import "AppDelegate.h"
#import "SWRevealViewController.h"
#import "HomePageViewController.h"
#import "SlideOutMenuTableViewController.h"
#import "SearchViewController.h"
#import "AboutUsViewController.h"
#import "SponsorsViewController.h"
#import "HowToDonateViewController.h"
#import "EmergencyViewController.h"
#implementation AppDelegate
#synthesize splitMenu,window,UINV;
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window = window;
HomePageViewController *HPVC = [[HomePageViewController alloc]init];
SlideOutMenuTableViewController *SOMTVC = SOMTVC = [[SlideOutMenuTableViewController alloc]init];
UINavigationController *frontNavigationController = [[UINavigationController alloc]initWithRootViewController:HPVC];
UINavigationController *rearNavigationController = [[UINavigationController alloc]initWithRootViewController:SOMTVC];
SWRevealViewController *revealController= [[SWRevealViewController alloc]initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate=self;
revealController.rightViewController = SOMTVC;
self.splitMenu = revealController;
self.window.rootViewController = self.splitMenu;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
HomePageViewController.m
//
// HomePageViewController.m
// IndianBloodBank
//
// Created by Roshith Balendran on 8/5/14.
// Copyright (c) 2014 Olympus. All rights reserved.
//
#import "HomePageViewController.h"
#import "SWRevealViewController.h"
#import "AboutUsViewController.h"
#import "SponsorsViewController.h"
#import "HowToDonateViewController.h"
#interface HomePageViewController ()
#end
#implementation HomePageViewController
#synthesize btn1SearchBlood,btn2DonateNow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.055 green:0.055 blue:0.059 alpha:1]];
self.navigationController.navigationBar.translucent=NO;
SWRevealViewController *revealViewController = [self revealViewController];
[revealViewController panGestureRecognizer];
[revealViewController tapGestureRecognizer];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"menu-icon"] style:UIBarButtonItemStyleBordered target:revealViewController action:#selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = menuButton;
}
The solution was simple, I just had to turn on ARC.
The basic requirement of SWRevealViewController Class is ARC enabled.
You cannot use this Class for slide out navigation with ARC disabled.
My bad guys. Thought I checked for errors with ARC enabled. My mistake.
So, if you encounter this error "#synthesize of 'weak' property is only allowed in ARC or GC Mode", just turn on ARC.

Can't seem to access my Core Data from a View Controller implementation

I am trying to create a very simple app that manipulates an iOS core data model I've named 'Person' with the attributes: 'age', 'firstName', and 'lastName'.
However it appears that as soon as I try to GET or Query this data model, iOS appears to be returning a nil value.
'NSInvalidArgumentException', reason: '+entityForName: nil is not a
legal NSManagedObjectContext parameter searching for entity name
'Person''
Here's what my view controller .m file looks like:
#import "bitcraft_v1ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "Person.h"
#interface bitcraft_v1ViewController ()
#end
#implementation bitcraft_v1ViewController
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
CLLocationManager *locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
NSLog(#"This is latitude: %f", latitude);
NSLog(#"This is longitude: %f", longitude);
Person *newPerson = [NSEntityDescription
insertNewObjectForEntityForName:#"Person"
inManagedObjectContext:self.managedObjectContext];
}
My view controller header file looks like this:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface bitcraft_v1ViewController : UIViewController
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//- (void)saveContext;
#property (strong, nonatomic) IBOutlet UILabel *regionCurrentlyEntered;
#end
And finally I haven't touched Person.h and Person.m very much:
Person.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Person : NSManagedObject
#property (nonatomic, retain) NSNumber * age;
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#end
Person.m
#import "Person.h"
#implementation Person
#dynamic age;
#dynamic firstName;
#dynamic lastName;
#end
I know that my view controller implementation file only declares the variable person but never actually stores a hardcoded first name, last name and age. But I can't seem to even get past this point.
Why is NSManagedObjectContext not able to find the entity Person in the Core Data? After finally declaring 'newPerson' Will I then be able to create "John Smith 42" and store this newPerson in the core data within the - (void)viewDidLoad?

Property 'images' not found on object of type 'UIViewController *'

I'm trying to merge these two projects.
Bearded - An iPhone photo app
Thumbnail Picker View
At this point I'm just trying to keep these in separate view controllers and getting the functionality of the Thumbnail Picker working in it's own controller.
I'm getting the error, as stated in the title, "Property 'images' not found on object of type 'UIViewController *'"
The error is coming from the 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]];
self.viewController = [[UIViewController alloc] init];
NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:nil];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString *path in paths) {
[images addObject:[UIImage imageWithContentsOfFile:path]];
}
self.viewController.images = images;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#end
Here is the AppDelegate.h:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
#end
I've tried declaring a property like so:
#property (strong, nonatomic) UIImageView *images;
I can't seem to fix this even though I painstakingly merged these two codebases.
Here is the Xcode project of where I'm at at this point:
Xcode project
the "images" property you've declared in your .h file:
#property (strong, nonatomic) UIImageView *images;
is not the NSMutableArray you are trying to assign to it via this line:
self.viewController.images = images;
If you can get the types to be the same (i.e. either a single UIImageView object or a NSMutableArray containing many UIImages), then you should have better luck with assigning things to that property.
I solved it. The problem was that I was merging a project using Storyboards and one that was using programmatically generated ViewController. Well, part of the problem.
Since, I merged the projects I had to give the view controller with the ThumbnailView another name. I named it ElfViewController.
This is the working code:
AppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#class ElfViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ElfViewController *viewController; // Changed here to ElfViewController
#property (strong, nonatomic) ViewController *firstViewController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "ElfViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ElfViewController alloc] init]; //Changed here to ElfViewController
self.firstViewController = [[ViewController alloc] init];
NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:nil];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString *path in paths) {
[images addObject:[UIImage imageWithContentsOfFile:path]];
}
self.viewController.images = images;
self.window.rootViewController = self.firstViewController;
[self.window makeKeyAndVisible];
}
#end
I'm still new to Objective-C and have only learned how to create ViewControllers in Storyboard and not programmatically.
Also I didn't know that you can declare a property to a specific View Controller rather than just a general UIViewContoller. That's another place I got tripped up.

iOS - CoreData - TableViewController - NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext

I created a storyboard with a UITableViewController, then added a Core Data entity. The application at this point built and run without errors, but the UITableViewController was showing no data.
I deleted the TVC and rebuilt in StoryBoard, but ever since I'm getting an error when I run the application and try to open the TVC:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a
legal NSManagedObjectContext parameter searching for entity name
'Attractions''
With a bit of research, realize that this is due to my managedObjectContext being empty, but for the life of me I cannot figure out WHY it's empty.
In the TVC header file:
#import <UIKit/UIKit.h>
#import "Attractions.h"
#import "AttractionListViewCell.h"
#import "ApplicationNameAppDelegate.h"
#interface AttractionListViewController : UITableViewController
{
NSManagedObjectContext *managedObjectContext;
NSMutableArray *AttractionsArray;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) NSMutableArray *AttractionsArray;
- (void) fetchrecords;
#end
In the TVC model file:
ApplicationNameAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
NSLog(managedObjectContext);
// Create connection to the DB via Context
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Attractions" inManagedObjectContext:managedObjectContext];
In the ApplicationNameAppDelegate.h file:
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
Any help or insight you could provide would be much appreciated.
EDIT - Added AppDelegate info:
#import <UIKit/UIKit.h>
#import "AttractionListViewController.h"
#import <CoreData/CoreData.h>
#class AttractionListViewController;
#interface AppNameAppDelegate : UIResponder <UIApplicationDelegate>
{
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
#property (strong, nonatomic) AttractionListViewController *viewController;
#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;
#end
This line:
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
You are declaring a local managedObjectContext and assigning it rather than what you should do:
managedObjectContext = [appDelegate managedObjectContext];
which will use the TVC's iVar
So I found what the issue was. I hadn't copied over all the classes from the AppDelegate in the xcode generated example, so the classes that defined the managedObject and persistent store etc weren't there.
In your View Controller implementation file, right under this bit of code:
- (void)viewDidLoad
{
add this bit of code:
id delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = [delegate managedObjectContext];

ios delegate doesn't fire

i try a simple Delegate but the method won't fire. Here is my code:
The view with the protocol and a button which should trigger the delegate:
ViewController.h
#import <UIKit/UIKit.h>
#protocol InitStackDelegate <NSObject>
#required
-(void)initstack:(NSInteger)amount;
#end
#interface ViewController : UIViewController{
IBOutlet UITextField *amountTextField;
__unsafe_unretained id<InitStackDelegate> delegate;
}
- (IBAction)init:(id)sender;
#property (nonatomic,assign)id delegate;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize delegate;
- (IBAction)init:(id)sender {
//send the delegate
[delegate initstack:[amountTextField.text intValue]];
}
AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate,InitStackDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic) ViewController *myView;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window;
#synthesize myView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//..... push second controller into navigation stack
myView.delegate = self;
return YES;
}
...
#pragma mark Delegate Method
-(void)initstack:(NSInteger)amount{
NSInteger test;
}
#end
When i hit the button i get to (IBAction) init but then the delegate do nothing. I set a breakpoint in AppDelegate.m but it is never reached. Any help?
thx
Mario
In you App Delegate, when you set myView.delegate=self myView doesn't actually point to anything!
You need to set myView to point to your ViewController.
Use this in didFinishLaunchingWithOptions
myView = (ViewController *)self.window.rootViewController;
myView.delegate = self;

Resources