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

时间:2016-01-10 08:14:29

标签: ios objective-c uitableview apple-push-notifications

我在我的应用上实施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]));
    }
}

出了什么问题?

2 个答案:

答案 0 :(得分:0)

=&GT;当你调用objectAtIndex:

时,确保你手上有一个数组

答案 1 :(得分:0)

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

它是从推送通知的aps对象创建的,该对象确实始终是字典,并使用密钥ReceivedNotifications保存在用户默认值中。

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

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