我的应用程序的一个核心功能是允许用户跟踪在某个位置花费的时间。但是,我遇到了(显然)一个严重的错误。我有两个核心数据实体:Location
用于保存用户的位置 - 并TimeSpentStudying
保存在该位置经过的时间(多对多关系)。我附上截图以使其更清楚:
http://i.imgur.com/C4VsWK2.png?1
这是我的 TimeSpentStudying 课程:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Location;
@interface TimeSpentStudying : NSManagedObject
@property (nonatomic, retain) NSString * libraryNameText;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSNumber * timeSpent;
@property (nonatomic, retain) Location *info;
@end
这是我将Location
实体传递给 viewController 的地方,它将跟踪经过的时间 - LibraryTrackTimeViewController
:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (distance < 500) {
if ([segue.identifier isEqualToString:@"TrackLibraryTimes"]) {
UINavigationController *navigationController = segue.destinationViewController;
LibraryTrackTimeViewController *controller = (LibraryTrackTimeViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
controller.libraryNameText = [[[mapView selectedAnnotations] objectAtIndex:0] title];
}
}
}
当我启动计时器时 - 收到此错误:
<TimeSpentStudying: 0x1e441130> (entity: TimeSpentStudying; id: 0x1d5a4260 <x-coredata:///TimeSpentStudying/t3B884C6F-8210-4B9E-A716-23DEC24291482> ; data: {
date = nil;
info = nil;
libraryNameText = nil;
timeSpent = 0;
})
2013-01-29 17:46:25.885 BooksonBooksonBooks[18856:907] -[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130
2013-01-29 17:46:25.892 BooksonBooksonBooks[18856:907] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130 with userInfo (null)
2013-01-29 17:46:25.899 BooksonBooksonBooks[18856:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TimeSpentStudying coordinate]: unrecognized selector sent to instance 0x1e441130'
这是我认为导致错误的方法:
-(IBAction)startTimer:(id)sender
{
startTime = [[NSDate alloc] init];
elapsedTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
TimeSpentStudying *timeSpentStudying = [NSEntityDescription insertNewObjectForEntityForName:@"TimeSpentStudying" inManagedObjectContext:self.managedObjectContext];
timeSpentStudying.timeSpent = [NSNumber numberWithDouble:totalTimeSpentStudying];
我不太明白这个错误。LibraryTrackTimeViewController
我是否必须专门为我创建全新的 managedObject 上下文TimeSpentStudying
?我希望每个Location
对象都TimeSpentStudying
保存多个 timeIntervals(日期、timeSpent 等)。我希望我说得足够清楚。如果您需要查看更多内容LibraryTrackTimeViewController
- 请告诉我,谢谢!