0

I hint error as below when i pass in data into PushNotificationManager.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI un_stringWithMaxLength:]: unrecognized selector sent to instance 0x604000018550'

Here is my code:-

     [self.manager GET:@"http://api.xxxx.com/api/promotion/1" parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            json_promotion = responseObject;

            [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

            NSString *strProTitle = [json_promotion valueForKey:@"title"];

            NSLog(@"strProTitle - %@",strProTitle);
            //Will get string "Promotion Today!" which is CORRECT
//If i put NSString *strProTitle = @"testing here", error will not appear.

            [[PushNotificationManager sharedInstance]graphicsPushNotificationWithTitle:strProTitle  subTitle:@"text here" body:@"desc here" identifier:@"2-1" fileName:@"Graphics.jpg" timeInterval:3 repeat:NO];

Any idea? Please help.

Test 87
  • 101
  • 2
  • 10
  • Avoid using `valueForKey:` if you are debuting. The issue is related to this https://stackoverflow.com/a/49666984/1801544 (for the error explaination) – Larme Apr 05 '18 at 09:29
  • Hi Larme, thanks for your hints. I avoid using valueForKey now. – Test 87 Apr 05 '18 at 11:40

1 Answers1

1

Your error message is basically telling you are passing an array instead of string.

I am guessing this is happening becuase valueForKey is returning an array. While parsing an object it's better to type check.

You could instead use json_promotion[0][@"title"].

If you want a better syntax I would use the following

NSString *strProTitle;
if([json_promotion isKindOfClass:[NSArray class]] {
  id obj = json_promotion[0][@"title"]
  if ([obj isKindOfClass: [NSString class]]) {
    strProTitle = obj;
  }
}
Rishab
  • 1,901
  • 2
  • 18
  • 34