我需要訪問被另一個類設置NSManagedObject,所以我嘗試做一個單身,看起來像這樣:試圖使單保存NSManagedObject,但似乎並不奏效
MyManager.h
#import <CoreData/CoreData.h>
#import <foundation/Foundation.h>
@interface MyManager : NSObject {
NSManagedObject *someProperty;
}
@property (nonatomic, retain) NSManagedObject *someProperty;
+ (id)sharedManager;
@end
MyManager.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);
進出口新的單身所以可以使用一些建議。
不要使用他們的東西喜歡這個。特別是沒有Core Data。被管理對象不是線程安全的。你可以在任何地方訪問單身人士,所以你永遠不能保證在正確的線程上。很多原因。只是不要這樣做。如果你需要在一些地方有一個託管對象,然後傳遞它。 – Fogmeister
@Fogmeister嗯,好像我被指示在先前的問題中使用單例。你可以看看並告訴我我該怎麼辦? http://stackoverflow.com/questions/19964589/difficulty-accessing-property-on-another-class – BluGeni
我的猜測是self.form已經是零,其餘似乎是正確的,但如果你把保留在你的@屬性而不是強這意味着你需要放置dealloc並調用超級dealloc,因爲你不使用弧。順便說一句,你應該使用弧! –