Cast id to a long type crashes

I'm trying to cast an id value, returned from a json, to a long value (timestamp purposes).

xcode sais me:

-[__NSCFString longValue]: unrecognized selector sent to instance 0x15d3f220 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString longValue]: unrecognized selector sent to instance 0x15d3f220'

The next lines break the code.

long time_now = [self getTimestamp]; long fdate = [[results2 objectForKey:@"pll_first_date"] longValue]; long sdate = [[results2 objectForKey:@"pll_second_date"] longValue];

What I'm doing wrong? id type is not a dynamic type to be casted?

After your comments, this is my possible solution:

long long time_now = [self getTimestamp]; long long fdate = [(NSString *)[results2 objectForKey:@"pll_first_date"] longLongValue]; long long sdate = [(NSString *)[results2 objectForKey:@"pll_second_date"] longLongValue];

-------------Problems Reply------------

This isn't a cast, it's a method invocation. And, you can't cast from id to long (well, you could, but it has very limited utility).

You're trying to ask an NSString to convert itself to a long, but it doesn't respond to longValue.

NSString does have a longLongValue method which you could use instead.

The selector is unrecognized because longValue is not a method in NSString. Use longLongValue instead.

long long fdate = [[results2 objectForKey:@"pll_first_date"] longLongValue];
long long nsdate = [[results2 objectForKey:@"pll_second_date"] longLongValue];

Here's a link to the documentation.

A possibility:

long long time_now = [self getTimestamp];
long long fdate = [(NSString *)[results2 objectForKey:@"pll_first_date"] longLongValue];
long long sdate = [(NSString *)[results2 objectForKey:@"pll_second_date"] longLongValue];

Category:ios Views:37 Time:2019-03-23

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.074 (s). 11 q(s)