我有一个单一类KTTTeacherService
,它有一个属性currentTeacher
。该属性最初为nil
,并使用静态方法KTTTeacherService#updateCurrentTeacher
进行更新。更新currentTeacher
后,当我尝试访问它时,我的应用程序因EXC_BAD_ACCESS
错误而崩溃。
KTTTeacherService.h
#import <Foundation/Foundation.h>
#import "KTTeacher.h"
@interface KTTTeacherService : NSObject
@property (nonatomic, assign) KTTeacher* currentTeacher;
+ (KTTTeacherService*)shared;
+ (KTTeacher*)currentTeacher;
+ (NSString*)apiKey;
+ (void)updateCurrentTeacher:(KTTeacher*) teacher;
- (KTTeacher*)retrieveCurrentTeacher;
@end
KTTTeacherService.m
#import "KTTTeacherService.h"
#import "KTTeacher.h"
static KTTTeacherService* _singleton = nil;
@implementation KTTTeacherService;
+ (KTTTeacherService*) shared {
if(_singleton == nil) {
_singleton = [[KTTTeacherService alloc] init];
}
return _singleton;
}
+ (KTTeacher*) currentTeacher {
KTTTeacherService* shared = [KTTTeacherService shared];
if(shared == nil) {
return nil;
}
return [shared retrieveCurrentTeacher];
}
+ (NSString*) apiKey {
KTTeacher* teacher = [KTTTeacherService currentTeacher];
return teacher == nil ? nil : teacher.apiKey;
}
+ (void) updateCurrentTeacher:(KTTeacher*) teacher {
[KTTTeacherService shared].currentTeacher = teacher;
}
- (KTTeacher*) retrieveCurrentTeacher {
return _currentTeacher;
}
@end
KTTeacher.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class KTClass, KTGroup, KTTeacherNote;
@interface KTTeacher : NSManagedObject
@property (nonatomic, strong) NSString * apiKey;
@property (nonatomic, strong) NSNumber * serverIdentifier;
@property (nonatomic, strong) NSString * firstName;
@property (nonatomic, strong) NSString * lastName;
@property (nonatomic, strong) NSString * photoPathOnServer;
@property (nonatomic, strong) NSString * username;
@property (nonatomic, strong) NSString * hashedPassword;
@property (nonatomic, strong) NSString * salt;
@property (nonatomic, strong) NSSet *classes;
@property (nonatomic, strong) NSSet *groups;
@property (nonatomic, strong) NSSet *notes;
@end
@interface KTTeacher (CoreDataGeneratedAccessors)
- (void)addClassesObject:(KTClass *)value;
- (void)removeClassesObject:(KTClass *)value;
- (void)addClasses:(NSSet *)values;
- (void)removeClasses:(NSSet *)values;
- (void)addNotesObject:(KTTeacherNote *)value;
- (void)removeNotesObject:(KTTeacherNote *)value;
- (void)addNotes:(NSSet *)values;
- (void)removeNotes:(NSSet *)values;
- (void)addGroupsObject:(KTGroup *)value;
- (void)removeGroupsObject:(KTGroup *)value;
- (void)addGroups:(NSSet *)values;
- (void)removeGroups:(NSSet *)values;
@end
KTTeacher.m
#import "KTTeacher.h"
#import "KTClass.h"
#import "KTTeacherNote.h"
@implementation KTTeacher
@dynamic apiKey;
@dynamic serverIdentifier;
@dynamic firstName;
@dynamic lastName;
@dynamic photoPathOnServer;
@dynamic username;
@dynamic hashedPassword;
@dynamic salt;
@dynamic classes;
@dynamic groups;
@dynamic notes;
@end
发生崩溃的地方:
KTTTeacherService* teacherService = [KTTTeacherService shared];
KTTeacher* currentTeacher = teacherService.currentTeacher;
具体来说:teacherService.currentTeacher
为什么会这样?在做了一些谷歌搜索之后,我发现了一些关于这个错误的信息是由访问垃圾收集的引用引起的,但我不明白为什么这个变量会被垃圾收集,因为单例保留了一个引用。
答案 0 :(得分:2)
属性写错了。使用强,弱,保留而不是分配
属性(非原子,赋值)KTTeacher * currentTeacher;
答案 1 :(得分:0)
我试图重现错误而我不能。创建一个项目并放置类KTTTeacherService,KTTeacher(带有apiKey属性的空类)一切正常。 因此,您必须在KTTeacher类中出现错误。
在viewDidLoad中,我有以下内容:
{{1}}
当你调用currentTeacher方法时,_currentTeacher(property)现在有一个值。