我尝试了 2 个不同的应用程序来测试一些东西。
第一个应用程序很简单
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSMutableString *text;
}
@property (nonatomic, retain)NSMutableString *text;
@end
视图控制器.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);
}
这使事物看起来像是用相同的名称合成,使它们成为相同的变量。导致它打印这个:
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
即使我使用 %p 打印 text 和 self.text 的地址内存,我也会得到相同的地址
*我的另一个应用测试是这样 *
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;
}
如果我这样做,这个应用程序将无法运行
masterVC.MOC = managedObjectContext;
但只有在我这样做的时候才能工作
masterVC.MOC = self.managedObjectContext;
即使我打印 managedObjectContext 和 self.managedObjectContext 的内存地址,我也会得到 2 个不同的地址
这怎么可能?2 个不同的应用程序中的同一件事,以 2 种不同的方式表现?!?!?!?!?!?!?!?!?