【问题标题】:How to pass dynamic string into PushNotificationManager如何将动态字符串传递给 PushNotificationManager
【发布时间】:2018-09-15 01:03:41
【问题描述】:

当我将数据传递到 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:。该问题与此stackoverflow.com/a/49666984/1801544 有关(用于错误说明)
  • 嗨拉姆,感谢您的提示。我现在避免使用 valueForKey。

标签: ios objective-c push-notification nsstring


【解决方案1】:

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

我猜这是因为valueForKey 正在返回一个数组。解析对象时最好进行类型检查。

您可以改用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;
  }
}

【讨论】:

  • 谢谢 Rishab,它的工作和你的建议很棒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2018-09-02
  • 2014-03-02
  • 2017-11-09
  • 1970-01-01
  • 2016-10-25
相关资源
最近更新 更多