-[__NSSingleObjectArrayI floatValue]: unrecognized selector sent to instance

290 Views Asked by At

Below is my Response

geolocation =         {
        loc =             {
            lat = "22.4409980000000004451976565178";
            long = "70.0686230000000023210304789245";
        };
    };  

My app crashes when I try below code:

NSNumber* n = [userPin valueForKeyPath:@"geolocation.loc.lat"];
NSLog(@"num is class %@", NSStringFromClass([n class]));
float fCost = [n floatValue];  

When I print my NSStringFromClass I get __NSSingleObjectArrayI.

Any suggestion how to fix this?

1

There are 1 best solutions below

2
CRD On

At a guess the (runtime) type of userPin is array and it contains one element. When applied to an array valueForKey:, on which valueForKeyPath: is based, returns an array produced by applying itself to every element of the array.

If this is the case either index userPin before the call or handle the array resulting from the call.

Addendum

Could you give me an example?

Trivial example:

// An array of strings
NSArray *test = @[ @"The", @"quick", @"brown", @"fox"];
// Trivial keypath...
NSArray *result = [test valueForKeyPath:@"length"];
// Print
NSLog(@"%@", result);

Produces:

(
    3,
    5,
    5,
    3
)

showing result is an array of the result of applying length to each element of `test. HTH