我试图获得两点之间的预设里程(school1& school2)。我写的名为“getMileage”的方法使用MagicalRecord正常工作,从我的CoreData中获取正确的里程(例如PointA到PointB是2.6英里)
我有一个带有2个组件的选择器 - 当用户从选择器中选择一个位置的名称时,我希望我的里程标签更新为适当的值(2.6,3.1,4.0,无论从数据库返回什么)
最初它加载正常 - 因为getMileage方法中的数组设置为空,因为没有在选择器上进行选择。一旦选择器选择了某个东西,它就会抛出一个错误,指出:
2014-02-18 10:31:41.345 54 Miles[18618:70b] Our result miles are: (
"2.6"
)
2014-02-18 10:31:41.345 54 Miles[18618:70b] -[__NSArrayI length]: unrecognized selector sent to instance 0x8a16690
2014-02-18 10:31:41.349 54 Miles[18618:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x8a16690'
*** First throw call stack:
如您所见 - NSLog显示从我的getMileage方法返回的相应里程。
以下是我的选择器的didSelectRow方法:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (_tripPicker) {
if (component == 0){
//Get Mileage array
NSArray *currentMiles = [self getMileage];
//Update Labels
begSchoolLabel.text = [_schoolArray1 objectAtIndex:row];
_milesLabel.text = [currentMiles valueForKey:@"miles"];
}
if (component == 1){
//Get Mileage array
NSArray *currentMiles = [self getMileage];
//Update Labels
endSchoolLabel.text = [_schoolArray2 objectAtIndex:row];
_milesLabel.text = [currentMiles valueForKey:@"miles"];
}
}
}
这是我的getMileage方法:
- (NSArray *)getMileage {
//Update the Mileage indicator to display miles between currently selected values
NSString *begSchool = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];
NSString *endSchool = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:1]];
NSPredicate *milesFilter = [NSPredicate predicateWithFormat:@"beg_school=%@ AND end_school=%@", begSchool, endSchool];
NSArray *resultMiles = [MetaMiles MR_findAllWithPredicate:milesFilter];
if (!resultMiles || ![resultMiles count]) {
//The first load - this displays appropriately in log letting us know there is currently nothing there
NSLog(@"Empty Array");
} else {
//This confirms our result miles come out appropriately - they look like this: ("2.1")
NSLog(@"Our result miles are: %@", [resultMiles valueForKey:@"miles"] );
}
return resultMiles;
}
问题出在我的选择器的didSelectRow中 - 当试图为labelMiles分配标签时;就在它破裂的时候。
我也尝试过这个'didSelectRow'方法:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (_tripPicker) {
if (component == 0){
//Get Mileage array
NSArray *currentMiles = [self getMileage];
//Update Labels
begSchoolLabel.text = [_schoolArray1 objectAtIndex:row];
_milesLabel.text = [currentMiles objectAtIndex:0];
}
if (component == 1){
//Get Mileage array
NSArray *currentMiles = [self getMileage];
//Update Labels
endSchoolLabel.text = [_schoolArray2 objectAtIndex:row];
_milesLabel.text = [currentMiles objectAtIndex:0];
}
}
}
这也开启了这个错误:
2014-02-18 10:44:02.099 54 Miles[18635:70b] Our result miles are: (
"2.6"
)
2014-02-18 10:44:02.099 54 Miles[18635:70b] -[MetaMiles length]: unrecognized selector sent to instance 0x8ad3960
2014-02-18 10:44:02.103 54 Miles[18635:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MetaMiles length]: unrecognized selector sent to instance 0x8ad3960'
*** First throw call stack:
我已经用了一段时间对抗这个问题并用Google搜索并搜索了答案,但却空手而归。
这可能是非常简单的我误解或不做。所有帮助表示赞赏!
答案 0 :(得分:1)
您的代码
NSArray *resultMiles = [MetaMiles MR_findAllWithPredicate:milesFilter];
if (!resultMiles || ![resultMiles count]) {
//The first load - this displays appropriately in log letting us know there is currently nothing there
NSLog(@"Empty Array");
} else {
//This confirms our result miles come out appropriately - they look like this: ("2.1")
NSLog(@"Our result miles are: %@", [resultMiles valueForKey:@"miles"] );
这对我来说看起来像resultMiles不是一个数组(可能是一个字典),因为你使用valueForKey
来获得结果,但是后来你使用的objectAtIndex
失败了