使用核心数据,如何初始化IVAR值? Analyzer给出了错误“消息表达式中的接收器是未初始化的值”
以下是我的代码片段,用于调用在分析器中引发问题的对象:
CDCamera *camForAnnotation;
for(CDCamera *cams in fetchedObjects){
NSSet *annotSet = cams.annotation;
for (CDAnnotations *myannos in annotSet){
if (annotationIdentifer == [myannos objectID]) {
// found camera for annotation
camForAnnotation = cams;
bearing = myannos.bearing;
break;
}
}
}
double aov = [camForAnnotation.aov doubleValue] //Receiver in message expression is an uninitialized value
这是我的CDCamera .h:
#import <CoreData/CoreData.h>
@class CDAnnotations;
@interface CDCamera : NSManagedObject
{
}
@property (nonatomic, retain) NSString * notes;
@property (nonatomic, retain) NSNumber * lfl;
@property (nonatomic, retain) NSNumber * ccd_h;
@property (nonatomic, retain) NSNumber * aov;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * monitor_d_size;
@property (nonatomic, retain) NSSet* annotation;
@end
@interface CDCamera (CoreDataGeneratedAccessors)
- (void)addAnnotationObject:(CDAnnotations *)value;
- (void)removeAnnotationObject:(CDAnnotations *)value;
- (void)addAnnotation:(NSSet *)value;
- (void)removeAnnotation:(NSSet *)value;
- (NSManagedObjectID *) getNSManagedObjectID;
@end
这是我的.m
#import "CDCamera.h"
#import "CDAnnotations.h"
@implementation CDCamera
@dynamic notes;
@dynamic lfl;
@dynamic ccd_h;
@dynamic aov;
@dynamic name;
@dynamic monitor_d_size;
@dynamic annotation;
- (NSManagedObjectID *) getNSManagedObjectID{
return [self objectID];
}
@end
感谢您的帮助。
答案 0 :(得分:2)
如果for循环中没有任何内容匹配,则无法保证在camForAnnotation中有值。
在声明它时应该将值初始化为nil,并且可能将后续代码包装在if语句中以保存运行任何内容,如果camForAnnotation仍为nil。
答案 1 :(得分:1)
菲尔,
Core Data具有在获取对象时调用的方法。这些用于自定义初始化。 -awakeFromFetch
,-awakeFromInsert
和-awakeFromSnapshotEvents:
是您应该检查的方法。
安德鲁