Hanny
Hanny

Reputation: 2159

Cannot get labels to update appropriately with array data from method

I am trying to get pre-set mileage between two points (school1 & school2). The method I wrote called "getMileage" works appropriately using MagicalRecord and fetches the right mileage from my CoreData (e.g. PointA to PointB is 2.6 miles)

I have a picker with 2 components on it - when the user selects a name of a location from either picker I want my miles label to update with the appropriate value (2.6, 3.1, 4.0, whatever is returned from the database).

Initially it loads fine - because the array set in the getMileage method is empty as no selections have been made on the picker. Once the picker selects something it throws an error which states:

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:

As you can see - the NSLog shows the appropriate miles returned from my getMileage method.

Here is the didSelectRow method of my picker:

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


    }
}

Here is my getMileage method:

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

The issue lies somewhere in the didSelectRow of my picker - when trying to assign the label the resultMiles ; that's when it breaks.

I have tried this as well for my 'didSelectRow' method:

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


    }
}

That kicks off this error as well:

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:

I've bashed my head against this for a while and googled and googled trying to find answers but have come up empty handed.

It's probably something very simple I'm misunderstanding or not doing. All help appreciated!

Upvotes: 0

Views: 76

Answers (1)

Flexicoder
Flexicoder

Reputation: 8501

Your code

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"] );

This to me looks like resultMiles is not an array (maybe a dictionary) as you are using valueForKey to get the result, but later on you are using objectAtIndex which fails

Upvotes: 1

Related Questions