错误“'NSInvalidArgumentException',原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x134d0a3c0'”

Error "'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x134d0a3c0'"

我正在我的应用程序上实施 Apple Push Notification Services。收到通知后,我想获取信息并将其添加到uitableview中。我按照这个 Pushbots tutorial 是这样的:

AppDelegate.m 文件中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [Pushbots sharedInstanceWithAppId:@"--my app id--"];
    [[Pushbots sharedInstance] receivedPush:launchOptions];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // This method will be called everytime you open the app
    // Register the deviceToken on Pushbots
    [[Pushbots sharedInstance] registerOnPushbots:deviceToken];
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Notification Registration Error %@", [error userInfo]);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //Handle notification when the user click it while app is running in background or foreground.
    [[Pushbots sharedInstance] receivedPush:userInfo];
    NSString *msg = [userInfo valueForKey:@"aps"];
    NSLog(@"Push Notification:%@",msg);

    [[NSUserDefaults standardUserDefaults]setObject:msg forKey:@"ReceivedNotifications"];
}

在我的 ViewController.m:

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *notifTableView;
@end

@implementation ViewController
{
    NSMutableArray *notif;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    notif = [[NSUserDefaults standardUserDefaults] objectForKey:@"ReceivedNotifications"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [notif count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [notif objectAtIndex:indexPath.row];
    return cell;
}

但是,我一直在控制台收到错误消息:

[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x134d0a3c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x134d0a3c0'
*** First throw call stack:
(0x180f45900 0x1805b3f80 0x180f4c61c 0x180f495b8 0x180e4d68c 0x100091b28 0x185f8931c 0x185f89484 0x185f787e8 0x185f8dfb0 0x185d2308c 0x185c33778 0x183642b2c 0x18363d738 0x18363d5f8 0x18363cc94 0x18363c9dc 0x1836360cc 0x180efc588 0x180efa32c 0x180e296a0 0x185ca6580 0x185ca0d90 0x100092a1c 0x1809ca8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的 main.m 文件:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

怎么了?

=> 确保在调用 objectAtIndex 时手上确实有一个数组:

错误消息清楚地表明 objectAtIndex 的接收者是一个 NSDictionary 对象。

它是从推送通知的 aps 对象创建的,它实际上始终是一个字典,并使用键 ReceivedNotifications.

保存在用户默认值中

您必须解析字典以提取要显示的信息。

PS:不要使用 valueForKey——这是一种具有特殊行为的 KVC 方法——从字典中获取对象。指定的方法是objectForKey