-4

I'm trying to convert a string to NSDate but I'm getting an error.

NSLog(@"JSON %@", responseObject);
NSArray *login = responseObject;
NSString *expires = [login valueForKey:@"expires"];
NSLog(@"%@", expires);

NSLog of responseObject (I used MMRecord 1.3 and AFNetworking 2)

<Login: 0x91dcdb0> (entity: Login; id: 0x91dcdf0 <x-coredata:///Login/t82A1AC69-501C-4C49-BAEE-C676FD655FDE2> ; data: {\n    \"access_token\" = \"******************\";\n    error = nil;\n    \"error_description\" = nil;\n    expires = \"Thu, 22 May 2014 02:30:21 GMT\";\n    \"expires_in\" = 1209599;\n    \"grant_type\" = nil;\n    issued = \"Thu, 08 May 2014 02:30:21 GMT\";\n    password = nil;\n    \"token_type\" = ***;\n    userName = \"su@email.in\";\n    userid = nil;\n})

When I NSLog that string expires I get something like this

(
"Thu, 22 May 2014 02:17:02 GMT"
)

If I do anything operation on that string, I'm getting this error

[__NSArrayI length]: unrecognized selector sent to instance 0x9754950

I think, for some reason, expires is of type NSArray. Can You tell me what wrong with this? How can I change it to NSDate? I'm new to iOS. Thanks in Advance!

user
  • 246
  • 2
  • 5
  • 12

2 Answers2

0

It looks like the key @"expires" refers to an array, not a string. If you can be sure that it's always an array and will always be non-empty, then you can fix with:

NSArray *expiresArray = login[@"expires"];
NSString *expires = expiresArray[0];

The next thing you'll want is an NSDate. See the answer here for that.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
-1
NSString *expires = [[login valueForKey:@"expires"] firstObject];
sonics876
  • 947
  • 2
  • 13
  • 31