2016-01-10 57 views
-1

我在我的應用程序上實現Apple Push Notification Services。獲得通知後,我想獲取信息並將其添加到可用視圖中。我跟着這個Pushbots tutorial這樣的:錯誤「'NSInvalidArgumentException',原因:' - [__ NSCFDictionary objectAtIndex:]:無法識別的選擇發送到實例0x134d0a3c0'」

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 file:

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

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

怎麼了?

回答

0

錯誤消息明確表明的objectAtIndex接收機爲NSDictionary對象。

它是從推送通知的aps對象創建的,它確實始終是字典,並用密鑰ReceivedNotifications保存在用戶默認值中。

你必須解析詞典來提取你想要顯示的信息。

PS:不要使用valueForKey - 這是一種具有特殊行爲的KVC方法 - 從字典中獲取對象。指定的方法是objectForKey

+0

如何將其更改爲數組對象? – Jay

+0

這取決於你要完成什麼。標準的'aps'字典包含'alert'和'badge'鍵。 'alert'本身就是一個有幾個鍵的字典,'badge'是一個整數。枚舉詞典並獲取所需的值和鍵。把它放入一個數組中。 – vadian

+0

你有代碼示例嗎?我試圖將手機收到的通知文本放入表格中。 – Jay

0

=>確保你真的在你的手中的數組,當你調用objectAtIndex:

相關問題