我需要访问在另一个类中设置的 NSManagedObject,所以我尝试制作一个如下所示的单例:
我的经理.h
#import <CoreData/CoreData.h>
#import <foundation/Foundation.h>
@interface MyManager : NSObject {
NSManagedObject *someProperty;
}
@property (nonatomic, retain) NSManagedObject *someProperty;
+ (id)sharedManager;
@end
我的经理.m
#import "MyManager.h"
@implementation MyManager
@synthesize someProperty;
#pragma mark Singleton Methods
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
someProperty = nil;
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
}
@end
我在这样的一类中设置:
MyManager *sharedManager = [MyManager sharedManager];
sharedManager.someProperty = self.form;
但是当我记录它时,它是空的
NSLog(@"shared::%@", sharedManager.someProperty);
我是单身人士的新手,所以可以使用一些建议。