Retrieving Content from PubNub Messages

I can successfully send and receive messages from PubNub, the problem occurs when I try to display the contents of the message and download it UITableViewCell UITextView.

Second TEST LOG writes out the entire message that I send from my iPhone (I already tried it using the Dev Console), but after that the application crashes.

[__NSCFDictionary length]: unrecognized selector sent to instance

I know that something is wrong with the dictionary, but I can not understand it. I use only one NSDictionaryfor the message that I send via PubNub, and it "arrives" to the console, so I think it works correctly. As you can see in the code, I tried some options, but without any success.

UPDATE

It works if I send NSString instead of NSDictionary.

@interface ViewController ()
@property (nonatomic, strong) NSString *myIncomeMessage;
@property (nonatomic, strong) NSString *messageFromDict;
@property (nonatomic, strong) NSArray *twoChannels;
@property (nonatomic, strong) NSDictionary *messagePbnb;
//@property (nonatomic, strong) PNMessage *messageNew;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    PNChannel *channel_2 = [PNChannel channelWithName:current.username shouldObservePresence:NO];
    PNChannel *channel_1 = [PNChannel channelWithName:self.messageRecipient shouldObservePresence:NO];

    [PubNub subscribeOnChannels:self.twoChannels];

    [PubNub requestHistoryForChannel:channel_1 from:nil to:nil limit:100 reverseHistory:YES];
    [PubNub requestHistoryForChannel:channel_2 from:nil to:nil limit:100 reverseHistory:YES];

    [[PNObservationCenter defaultCenter] addMessageReceiveObserver:self withBlock:^(PNMessage *message) {
        NSLog(@"OBSERVER: Channel: %@, Message: %@", message.channel.name, message.message);
        NSLog(@"Sample TEST LOG %@", message.message);

        self.myIncomeMessage = message.message;
        NSLog(@"Second TEST LOG %@", self.myIncomeMessage);
   //   self.messageFromDict = [NSString stringWithFormat:keyMessage, self.messagePbnb];
   //   self.messageFromDict = [NSString stringWithFormat:keyMessage, message];
    }];

    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(reloadTable) userInfo:nil repeats:YES];

    [self setupUIForInput];

}


- (IBAction) inputContent {

    NSString *messageContent = self.textView.text;

    PNChannel *channel_1 = [PNChannel channelWithName:self.messageRecipient shouldObservePresence:NO];
    PNChannel *channel_2 = [PNChannel channelWithName:senderUser.username shouldObservePresence:NO];

    self.twoChannels = @[channel_1,channel_2];

    [PubNub subscribeOnChannels: self.twoChannels];

    self.messagePbnb = @{ @"keyMessage": messageContent, @"keySenderUser": self.senderUser.username, @"keyRecieverChannel": self.messageRecipient} ;

    [PubNub sendMessage: self.messagePbnb toChannel:channel_1];
    [PubNub sendMessage: self.messagePbnb toChannel:channel_2];
    [self.textView resignFirstResponder];
    [self reloadInputViews];

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    OutputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellThree"];

    cell.textOutput.text = self.myIncomeMessage;

-(void)reloadTable{
    [tableViewThree reloadData];
}
+4
1

@sabin, , - :

[[PNObservationCenter defaultCenter] addMessageReceiveObserver:self
                                                     withBlock:^(PNMessage *message) {

     id messageData = message.message;
     if ([messageData isKindOfClass:[NSDictionary class]]) {

     NSString *messageString = [NSString stringWithFormat:@"foo: %@, and bar: <%@>",
             [(NSDictionary *)messageData valueForKey:@"foo"],
             [(NSDictionary *)messageData valueForKey:@"bar"]];
     }
 }];

- (pubnub.com/console):

{"foo":"hey", "bar":"you!"}

( messageString), :

foo: hey, and bar: you!

, !

+3

Source: https://habr.com/ru/post/1542552/


All Articles