views:

128

answers:

1

Hello,

I'm getting the above error and have no idea where its happening. Is there any way to debug it. I've put in some breaks points already buts its not abending its not abending those areas.

Code below:

    -(void)viewDidLoad 
    { 
     if (managedObjectContext == nil) 
     { 
            managedObjectContext = [(NewWorkoutViewController *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
            NSLog(@"After managedObjectContext: %@",  managedObjectContext);
     }

     //Setup PickerView values.  
     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:1.0];
     CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 480);
     pickerView.transform = transform;
     [UIView commitAnimations];
     // When the view loads, run this action.
     [super viewDidLoad]; // This NEEDS to be here or the application will not work!
     route.delegate = self;

     //Initialise variable.
     counterInt = 0;
    } 


    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
     UITouch *touch;
     touch=[touches anyObject];
     CGPoint point=[touch locationInView:self.view];

     if (CGRectContainsPoint([route frame],point))
     {
      [self routePickerShow];
     }

     if (CGRectContainsPoint([activity frame],point))
     {
      [self activityPickerShow];
     }

     if (CGRectContainsPoint([intensity frame],point))
     {
      [self intensityPickerShow];
     }
    }


-(IBAction)routePickerShow
{  
 NSFetchRequest *request = [[NSFetchRequest alloc] init];

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Route" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];

 NSError *error = nil;
 NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];

 // Check for errors from the FetchRequest
 if (nil == results || nil != error)
  NSLog(@"Error getting results : %@", error);

 routeArray = [results mutableCopy];
 [request release];

 int arrayItemQuantity = [routeArray count];
 NSLog(@"Array Quantity: %d", arrayItemQuantity);
 int i;

    for (i = 0; i < arrayItemQuantity; i++)
 {  
  NSLog(@"Element %i: %@", i, [[routeArray objectAtIndex:i] valueForKey:@"route_name"]);
  NSLog(@"Element %i: %@", i, [[routeArray objectAtIndex:i] valueForKey:@"route_id"]);
 }

 // Initialise PickerView details.
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.5];
 CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
 pickerView.transform = transform;
 [self.view addSubview:pickerView];
 [UIView commitAnimations]; 
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)routePicker
{
 return 1;
}


-(NSInteger)pickerView:(UIPickerView *)routePicker numberOfRowsInComponent:(NSInteger)component
{
 return [routeArray count];
}


-(NSString *)pickerView:(UIPickerView *)routePicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 return [routeArray objectAtIndex:row];
}


-(void)pickerView:(UIPickerView *)routePicker didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"];
 mapID = [[routeArray objectAtIndex:row] valueForKey:@"route_id"];
 NSLog(@"Map ID NW: %@", mapID);

 //activity.text = [list objectAtIndex:row];
 //intensity.text = [list objectAtIndex:row];
}

All I know is that its not getting as far a the method pickerView, it abends before this.

+2  A: 

Your specific error is that you have a string that you are trying to set to a Core Data object. Is route_id an object or a string? Is mapID a string? It could be this line:

NSLog(@"Element %i: %@", i, [[routeArray objectAtIndex:i] valueForKey:@"route_id"]);

Also, you might want to add a breakpoint to catch uncaught exceptions. Open the breakpoints window. Select the Global Breakpoints group so you only have to do this once and it will be in all projects. Add objc_exception_throw to the break points and you will catch all exceptions before they crash your code. Also, enable NSZombies in your debug code. See here and here for some info.

Don
route_id is an object and mapID is a string. I commented out all the code in this section and just put in an NSLog statement, but I'm still getting the same error.
Stephen
If mapID is a string and route_id is a NSManagedObject, this line: ` mapID = [[routeArray objectAtIndex:row] valueForKey:@"route_id"];` would cause that error as the runtime tries to get a string value from the managed object. But you commented that out. What's the last line of code that gets run when stepping through? Did you set the objc_exception_throw breakpoint? What does the call stack look like when it's hit?
Don
Don, I just realised you were referring to the line of code in -(IBAction)routePickerShowThis particular like of code is working with no problems, details are displayed in the Console.
Stephen
Yes I've set the objc_exception_throw breakpoint. How do I check the call stack, I'm not that familar with the debugger.Does this make any sense:0x94feb4e6 <+0021> mov 0x8(%ebp),%esi
Stephen
Yes, that was me being dumb - %@ will use `description` and of course NSManagedObjects have that property. What should be displayed in your pickerView? If it's route_id, you may need to display an NSString representation instead.
Don
Not sure how to check the last line of code that ran either.
Stephen
route_name is what I want displayed in pickerView.
Stephen
That's the disassembly. You want a list of methods, under a title that probably says "Thread-1". Do you see that? Check out http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeDebugging/100-Debugging_in_the_Debugger/debugging_in_debugger.html for a visual.
Don
#0 0x94feb4e6 in objc_exception_throw#1 0x021b183b in -[NSObject doesNotRecognizeSelector:]#2 0x02148676 in ___forwarding___#3 0x021246c2 in __forwarding_prep_0___#4 0x007dabef in -[UILabel setText:]#5 0x00730e06 in -[UIImageAndTextTableCell setTitle:]#6 0x006d0f71 in -[UIPickerView table:cellForRow:column:reusing:]#7 0x006cc36d in -[UIPickerView table:cellForRow:column:]
Stephen
Set a break point where you think it is getting to, then step over the code (Command-Shift-O) until you get the exception. Set breakpoints anywhere you want to see if the code is hit. They can't hurt. You can resume execution after hitting the breakpoint by hitting Command-Option-P.
Don
Great! So the problem is when the picker view is trying to set the text to display. It is setting a NSManagedObject instead of a string. Where are you setting the title for each cell?
Don
The only place I think of is:-(void)pickerView:(UIPickerView *)routePicker didSelectRow:(NSInteger)row inComponent:(NSInteger)componentwhere the line is: route.text = [[routeArray objectAtIndex:row] valueForKey:@"route_name"];but if I comment out this line I still get an error, so I'm not quite sure what's going on.
Stephen
It sets the title here: `-(NSString *)pickerView:(UIPickerView *)routePicker titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [routeArray objectAtIndex:row];}` In there, you are returning the object at the row, when you want to return `[[routeArray objectAtIndex:row] valueForKey:@"route_name"]`.
Don
That's it Don, problem solved....thanks very much for you help and patients.Regards,Stephen
Stephen