iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 从PubNub消息中检索内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以使用PubNub成功发送和接收消息,当我尝试显示消息中的内容并加载UITableViewCell的UITextView时会出现问题.

第二个TEST LOG写出了我从iPhone发送的整个消息(我已经使用开发控制台尝试过了),但在此之后应用程序崩溃了.

[__NSCFDictionary length]: unrecognized SELEctor sent to instance

我知道字典有问题,但我无法弄清楚.我只使一个NSDictionary作为我通过PubNub发送的消息,它“到达”控制台,因此我认为它正常工作.正如你在代码中看到的那样,我尝试了一些变化,但没有任何成功.

updatE

如果我发送NSString而不是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];
}

解决方法

@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"]];
     }
 }];

如果您随后从Web控制台(pubnub.com/consolE)发布了此内容

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

您将能够从上面的代码(通过messageString)呈现一个字符串,如下所示:

foo: hey,and bar: you!

如果有帮助,请告诉我!

大佬总结

以上是大佬教程为你收集整理的ios – 从PubNub消息中检索内容全部内容,希望文章能够帮你解决ios – 从PubNub消息中检索内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。