如何将动态字符串传递给PushNotificationManager

时间:2018-04-05 09:10:08

标签: ios objective-c push-notification nsstring

当我将数据传入PushNotificationManager时,我提示如下错误。

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

这是我的代码: -

     [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];

有什么想法吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

您的错误消息基本上是在告诉您正在传递数组而不是字符串。

我猜这种情况正在发生,因为valueForKey正在返回一个数组。在解析对象时,最好键入check。

您可以使用json_promotion[0][@"title"]

如果您想要更好的语法,我会使用以下

NSString *strProTitle;
if([json_promotion isKindOfClass:[NSArray class]] {
  id obj = json_promotion[0][@"title"]
  if ([obj isKindOfClass: [NSString class]]) {
    strProTitle = obj;
  }
}