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

How to pass dynamic string into PushNotificationManager

当我将数据传入 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];

有什么想法吗?请帮忙。

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

我猜这是因为 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;
  }
}