【问题标题】:Trying to save elapsedTime to coreData试图将 elapsedTime 保存到 coreData
【发布时间】:2013-01-29 23:01:08
【问题描述】:

我的应用程序的一个核心功能是允许用户跟踪在某个位置花费的时间。但是,我遇到了(显然)一个严重的错误。我有两个核心数据实体: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 中专门为 TimeSpentStudying 创建全新的 managedObject 上下文?我希望每个Location 对象都保存多个TimeSpentStudying timeIntervals(日期、timeSpent 等)。我希望我说得足够清楚。如果您需要查看更多 LibraryTrackTimeViewController - 请告诉我,谢谢!

【问题讨论】:

  • This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. 那么,你有吗?如果是这样,你能添加它的代码吗?

标签: iphone ios core-data nsmanagedobjectcontext


【解决方案1】:

错误在这里:

BooksonBooksonBooks[18856:907] -[TimeSpentStudying 坐标]:无法识别的选择器发送到实例 0x1e441130

您在 TimeSpentStudying 类的对象上调用“坐标”,它没有实现该方法,这会导致应用崩溃。

由于我在您发布的任何代码中都没有看到坐标,并且您说您的其他实体类型是位置,它可能包含一个坐标,因此您可能正在传递一个您打算通过时间花费研究的位置。你在使用 ARC 吗?

【讨论】:

    猜你喜欢
    • 2015-01-05
    • 2019-09-16
    • 2021-05-27
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多