NSManagedObject子类符合协议?

问题描述:

我的主要问题是存储Core Data不支持的数据。我已经有一个CLLocation属性存储为可转换属性。我认为正确的方法是声明一个瞬态坐标属性。但是我不断收到EXC_BAD_ACCESS错误。NSManagedObject子类符合<MKAnnotation>协议?

编辑:

我现在的子类有如下界面:

#import <Foundation/Foundation.h> 
#import <CoreLocation/CoreLocation.h> 

@interface Event : NSManagedObject { 

} 

@property (nonatomic, retain) NSString* title; 
@property (nonatomic, retain) NSDate* timeStamp; 
@property (nonatomic, retain) CLLocation *location; 

@end 

所以我需要添加

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate; 

,以符合协议。 (setCoordinate是可选的,但如果我想要注释可拖动,则需要它)

在核心数据中,位置属性是可变形的。我在实现中使用@dynamic来生成访问器。我在整个代码中使用这个属性,所以我不想保留这个。

我认为最好的解决方法是将核心数据中的坐标属性定义为瞬态,但我并不确定是否在执行时出错。

- (CLLocationCoordinate2D)coordinate { 
    CLLocationCoordinate2D cor = CLLocationCoordinate2DMake(self.location.coordinate.latitude, 
    self.location.coordinate.longitude); 
    return cor; 
} 

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate { 
    CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude 
    longitude:newCoordinate.longitude]; 
    [self.location release]; 
    self.location = newLoc; 
} 

我试了几种方法,但这是最近的一种。

编辑2: 的EXC_BAD_ACCESS在:

_kvcPropertysPrimitiveSetters 
+0

你真的应该提供更多的细节,除非你只是问你是否可以应用协议。 – TechZen 2010-07-20 14:21:16

+0

编辑更多的细节。感谢您的帮助 – Derrick 2010-07-20 18:58:46

,您可以拨打NSManagedObject子类符合你想,只要协议不以某种方式覆盖实例的上下文的管理的任何协议。 MKAnnotation协议应该是完全安全的。

更新:

你的问题很可能是在这里:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate { 
    CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude 
                longitude:newCoordinate.longitude]; 
    [self.location release]; //<-- Don't release properties! 
    self.location = newLoc; 
} 

发电机存取将处理保留为您服务。当你直接释放它们时,你可以将这个管理程序搞定你也在泄漏newLoc。尝试:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate { 
    CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:newCoordinate.latitude 
                longitude:newCoordinate.longitude]; 
    self.location = newLoc; 
    [newLoc release]; 
} 

这将是很好的知道你在哪里得到EXC_BAD_ACCESS错误,但是这里有几个想法。首先,假设有一个外部班级想要拨打setCoordinate:,那么您应该更改@property列表coordinatereadwrite,因为您正在为世界改变他们不允许更改此值。您可以尝试的另一件事是继续并实际发送coordinatesetCoordinate:然后您可以消除您的自定义coordinate方法,并允许Core Data为您写一个更快的方法。

+0

我有只读,因为Apple Docs说它必须是 http://developer.apple。com/iphone/library/documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html#// apple_ref/occ/intfp/MKAnnotation/coordinate 另外,我相信核心数据要求您在属性瞬变时自己实现访问器。 (纠正我,如果我错了) 看起来我错过了一些关于KVC与我目前的实施。 – Derrick 2010-07-20 21:04:33