Riccardo
Riccardo

Reputation: 31

ivar behave in 2 different way, with same usage

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?!?!?!?!?!?!?!?!?

Upvotes: 0

Views: 42

Answers (1)

Puneet Sharma
Puneet Sharma

Reputation: 9484

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.

Upvotes: 1

Related Questions