好吧,所以即時製作一個全球定位系統跟蹤應用程序,跟蹤用戶的所有代碼工作良好,但現在即時通訊嘗試傳遞數據保存並顯示在彙總視圖。我有一個在模型中正確設置的實體Run,我意識到我需要使用options方法向appDelegate.m didFinishLaunching添加一些內容,以便managedObjectContext被正確初始化,但我似乎無法弄清楚。任何幫助將被大力讚賞。核心數據,enitityForName零誤差
這裏是我的錯誤
終止應用程序由於未捕獲的異常 「NSInvalidArgumentException」的,理由是:「+ entityForName:無不是 法律的NSManagedObjectContext參數搜索實體名稱 ‘運行’」
這裏是我的應用程序delegate.m
'import "AppDelegate.h"
#import <CoreData/CoreData.h>
#import "SummaryViewController.h"
#import "CardioViewController.h"
@interface AppDelegate()
@end
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Beginner Fitness" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Beginner Fitness.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end'
這裏是我的CardioViewController,我使用了斷點,錯誤發生在第一次提到「Run」時saveRun方法的開始處,正如您對錯誤的期望。
static NSString * const detailSegueName = @"NewRunDetails";
@interface CardioViewController()<UIActionSheetDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
@property int seconds;
@property float distance;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSMutableArray *locations;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) Run *run;
@property (nonatomic, weak) IBOutlet UILabel *timeLabel;
@property (nonatomic, weak) IBOutlet UILabel *distLabel;
@property (nonatomic, weak) IBOutlet UILabel *paceLabel;
@property (nonatomic, weak) IBOutlet UIImageView *progressImageView;
@property (nonatomic, weak) IBOutlet UIButton *startButton;
@property (nonatomic, weak) IBOutlet UIButton *stopButton;
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end
@implementation CardioViewController{
NSMutableArray *locationsArray;
}
@synthesize mapView = _mapView;
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.title = _DetailModal2[0];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.timer invalidate];
}
#pragma mark - IBActions
-(IBAction)startPressed:(id)sender
{
// hide the start UI
// show the running UI
self.seconds = 0;
// initialize the timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(eachSecond) userInfo:nil repeats:YES];
self.distance = 0;
self.locations = [NSMutableArray array];
[self startLocationUpdates];
}
- (IBAction)stopPressed:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save", @"Discard", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
#pragma mark - Private
- (void)saveRun
{
Run *newRun = [NSEntityDescription insertNewObjectForEntityForName:@"Run" inManagedObjectContext:self.managedObjectContext];
newRun.distance = [NSNumber numberWithFloat:self.distance];
newRun.duration = [NSNumber numberWithInt:self.seconds];
newRun.timestamp = [NSDate date];
NSMutableArray *locationArray = [NSMutableArray array];
for (CLLocation *location in self.locations) {
Location *locationObject = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:self.managedObjectContext];
locationObject.timestamp = location.timestamp;
locationObject.latitude = [NSNumber numberWithDouble:location.coordinate.latitude];
locationObject.longitude = [NSNumber numberWithDouble:location.coordinate.longitude];
[locationArray addObject:locationObject];
}
newRun.locations = [NSOrderedSet orderedSetWithArray:locationArray];
self.run = newRun;
// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
- (void)eachSecond
{
self.seconds++;
[self updateProgressImageView];
[self updateLabels];
}
- (void)updateProgressImageView
{
int currentPosition = self.progressImageView.frame.origin.x;
CGRect newRect = self.progressImageView.frame;
switch (currentPosition) {
case 20:
newRect.origin.x = 80;
break;
case 80:
newRect.origin.x = 140;
break;
default:
newRect.origin.x = 20;
break;
}
self.progressImageView.frame = newRect;
}
- (void)updateLabels
{
self.timeLabel.text = [NSString stringWithFormat:@"Time: %@", [MathController stringifySecondCount:self.seconds usingLongFormat:NO]];
self.distLabel.text = [NSString stringWithFormat:@"Distance: %@", [MathController stringifyDistance:self.distance]];
self.paceLabel.text = [NSString stringWithFormat:@"Pace: %@", [MathController stringifyAvgPaceFromDist:self.distance overTime:self.seconds]];
}
- (void)startLocationUpdates
{
// Create the location manager if this object does not
// already have one.
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
// Movement threshold for new events.
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.locationManager stopUpdatingLocation];
// save
if (buttonIndex == 0) {
[self saveRun];
[self performSegueWithIdentifier:detailSegueName sender:nil];
// discard
} else if (buttonIndex == 1) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
for (CLLocation *newLocation in locations) {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20) {
// update distance
if (self.locations.count > 0) {
self.distance += [newLocation distanceFromLocation:self.locations.lastObject];
CLLocationCoordinate2D coords[2];
coords[0] = ((CLLocation *)self.locations.lastObject).coordinate;
coords[1] = newLocation.coordinate;
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 500, 500);
[self.mapView setRegion:region animated:YES];
[self.mapView addOverlay:[MKPolyline polylineWithCoordinates:coords count:2]];
}
[self.locations addObject:newLocation];
}
}
}
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline *polyLine = (MKPolyline *)overlay;
MKPolylineRenderer *aRenderer = [[MKPolylineRenderer alloc] initWithPolyline:polyLine];
aRenderer.strokeColor = [UIColor blueColor];
aRenderer.lineWidth = 3;
return aRenderer;
}
return nil;
}
- (void)dealloc
{
//
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.locationManager.delegate = nil;
}
- (void)initilizeLocationTracking
{
_locationManager = [[CLLocationManager alloc] init];
assert(self.locationManager);
self.locationManager.delegate = self; // tells the location manager to send updates to this object
//
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
// start tracking the user's location
[self.locationManager startUpdatingLocation];
}
- (MKCoordinateRegion)coordinateRegionWithCenter:(CLLocationCoordinate2D)centerCoordinate approximateRadiusInMeters:(CLLocationDistance)radiusInMeters
{
// Multiplying by MKMapPointsPerMeterAtLatitude at the center is only approximate, since latitude isn't fixed
//
double radiusInMapPoints = radiusInMeters*MKMapPointsPerMeterAtLatitude(centerCoordinate.latitude);
MKMapSize radiusSquared = {radiusInMapPoints,radiusInMapPoints};
MKMapPoint regionOrigin = MKMapPointForCoordinate(centerCoordinate);
MKMapRect regionRect = (MKMapRect){regionOrigin, radiusSquared}; //origin is the top-left corner
regionRect = MKMapRectOffset(regionRect, -radiusInMapPoints/2, -radiusInMapPoints/2);
// clamp the rect to be within the world
regionRect = MKMapRectIntersection(regionRect, MKMapRectWorld);
MKCoordinateRegion region = MKCoordinateRegionForMapRect(regionRect);
return region;
}
- (IBAction)setMap:(id)sender {
switch (((UISegmentedControl*) sender).selectedSegmentIndex) {
case 0:
_mapView.mapType = MKMapTypeStandard;
break;
case 1:
_mapView.mapType = MKMapTypeSatellite;
break;
case 2:
_mapView.mapType = MKMapTypeHybrid;
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:detailSegueName]) {
[[segue destinationViewController] setRun:self.run];
}
}
乾杯夥計,這個工作對我來說:),得到一個數組越界異常,但現在生病揣摩原因再次發帖之前,謝謝。 – fredEtch
很好聽,所以接受答案:-) – thorb65