1

I am implementing a login system that uses the NSNotifications userInfo dictionary to pass login info by notification. The dictionary gets passed ok but when I try to convert one of the NSStrings in the dictionary into an int using IntValue I get error. Even when I copy the dictionary object to another string I get the same error, but with a normal string there are no errors. Code:

- (void)loginComplete:(NSNotification *) notification {
    if ([[notification name] isEqualToString:@"Login Complete"]) {
        NSDictionary *loginInfo = [notification userInfo];

        loginString = [loginInfo objectForKey:@"login_string"];
        NSLog(@"%@", loginString);
        NSString* expString = [NSString stringWithString:[loginInfo objectForKey:@"expires_in"]];
        // [loginInfo objectForKey:@"expires_in"] == @"604700"
        expiresIn   = [expString intValue];
        NSLog(@"expiresIn: %i", expiresIn);

        NSString * toInt = @"112345";
        int realInt = [toInt intValue];
        NSLog(@"realInt: %i", realInt);
    }
}

So the first NSLog provides correct info, and the conversion in the end (test) also works, but the expiresIn results in an error:

2015-01-13 17:53:06.976 API_test_osx[2867:143430] -[__NSArrayM length]: unrecognized selector sent to instance 0x600000441d40
2015-01-13 17:53:06.976 API_test_osx[2867:143430] *** WebKit discarded an uncaught exception in the webView:didFinishLoadForFrame: delegate: <NSInvalidArgumentException> -[__NSArrayM length]: unrecognized selector sent to instance 0x600000441d40

If I try not to convert to int, the value can be logged just fine as a NSString. What am I doing wrong?

Chris
  • 7,270
  • 19
  • 66
  • 110
MarekB
  • 13
  • 4
  • Can you show how you set a userInfo of NSNotification? Look this question maybe help: http://stackoverflow.com/questions/7896646/how-to-pass-object-with-nsnotificationcenter NSArray doens't have objectForKey! – weso Jan 13 '15 at 16:16

2 Answers2

2

The object you think is an NSString is in fact an NSArray as demonstrated by the error message:

-[__NSArrayM length]: unrecognized selector sent to instance 0x600000441d40

I cannot provide more information than that, other to say that if you are passing around integers within an Objective-C collection class then use an NSNumber object, not an NSString object.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • That makes sense, thank you. The third party code I used made the values into arrays of 1 length. What kept me from getting it was that I was still able to assign the values into NSString ivars, propably because it called implicitly the NSArrays toString method. I mark your answer as the right one. – MarekB Jan 13 '15 at 16:27
  • Also prefer using 'NSInteger' rather than 'int' and 'integerValue' rather than 'intValue' – KirkSpaziani Jan 13 '15 at 17:05
0

It looks like your accessing an array of objects under the key you specify. You will also need to access the objects index value. Ie objectAtIndex. Hope this helps

Tom
  • 2,358
  • 1
  • 15
  • 30