【问题标题】:[NSManagedObject isEqualToString:]: unrecognized selector sent to instance 0x454a590'[NSManagedObject isEqualToString:]:无法识别的选择器发送到实例 0x454a590'
【发布时间】:2010-07-26 14:53:22
【问题描述】:

我收到上述错误,但不知道它发生在哪里。有什么办法可以调试。我已经设置了一些断点,但它没有改变它没有改变这些区域。

代码如下:

    -(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];
}

我所知道的是它没有达到方法pickerView,它在此之前异常终止。

【问题讨论】:

    标签: iphone xcode core-data


    【解决方案1】:

    您的具体错误是您尝试将字符串设置为 Core Data 对象。 route_id 是对象还是字符串? mapID 是字符串吗?可能是这一行:

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

    此外,您可能需要添加断点来捕获未捕获的异常。打开断点窗口。选择 Global Breakpoints 组,这样您只需执行一次,它将在所有项目中。将 objc_exception_throw 添加到断点,您将在所有异常崩溃之前捕获所有异常。此外,在您的调试代码中启用 NSZombies。有关一些信息,请参阅 herehere

    【讨论】:

    • route_id 是一个对象,mapID 是一个字符串。我注释掉了本节中的所有代码,并添加了一个 NSLog 语句,但我仍然收到同样的错误。
    • 如果 mapID 是一个字符串并且 route_id 是一个 NSManagedObject,那么这一行:`mapID = [[routeArray objectAtIndex:row] valueForKey:@"route_id"];` 会在运行时尝试从托管对象中获取字符串值。但是你把它注释掉了。单步执行时运行的最后一行代码是什么?您是否设置了 objc_exception_throw 断点?调用堆栈被命中时是什么样子的?
    • 唐,我刚刚意识到您指的是 -(IBAction)routePickerShow 中的代码行 这个特殊的代码运行没有问题,详细信息显示在控制台中。
    • 是的,我设置了 objc_exception_throw 断点。如何检查调用堆栈,我对调试器不太熟悉。这是否有意义:0x94feb4e6 mov 0x8(%ebp),%esi
    • 是的,那是我很笨 - %@ 将使用 description,当然 NSManagedObjects 具有该属性。您的pickerView 中应该显示什么?如果是 route_id,您可能需要改为显示 NSString 表示。
    猜你喜欢
    • 2013-01-17
    • 2011-10-16
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多