gpt4 book ai didi

ios - 从 Parse 查询中访问 NSArray

转载 作者:行者123 更新时间:2023-11-28 21:52:27 24 4
gpt4 key购买 nike

在我的查询中,NSLog 很好,它写了好东西 NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages,但是查询在 viewDidLoad 中,我无法从我的 NSArray 访问值

NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
NSLog(@"EN DEHORS QUERY : %@", self.messages);

我必须实现@synthesize 吗?我做错了什么?

这是我的代码。

聊天 View Controller .h :

#import "MessagesViewController.h"

@interface ChatViewController : MessagesViewController

@property (strong, nonatomic) NSMutableArray *messages;
@property (strong, nonatomic) NSString *destinateurChat;
@property (strong, nonatomic) NSArray *senderMsg;
@property (strong, nonatomic) NSArray *destinateurMsg;

@end

聊天 View Controller .m :

#import "ChatViewController.h"
#import <Parse/Parse.h>
#import "SVProgressHUD.h"

@interface ChatViewController ()

@end
id message;
NSDate *receiveDate;
NSString *text;
@implementation ChatViewController
@synthesize destinateurMsg = _destinateurMsg;
@synthesize messages = _messages;

#pragma mark - View lifecycle
- (void)viewDidLoad
{

[super viewDidLoad];
self.title = @"Messages";
PFQuery *query1 = [PFQuery queryWithClassName:@"Chat"];
[query1 whereKey:@"DestinateurId" equalTo:self.destinateurChat];
[query1 whereKey:@"senderId" equalTo:[[PFUser currentUser] objectId]];
PFQuery *query2 = [PFQuery queryWithClassName:@"Chat"];
[query2 whereKey:@"DestinateurId" equalTo:[[PFUser currentUser] objectId]];
[query2 whereKey:@"senderId" equalTo:self.destinateurChat];
// PFQuery *hotelQuery = [PFQuery orQueryWithSubqueries:@[query1, query2]];

//Message envoyé par l'user
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_senderMsg = [objects valueForKey:@"text"];
_messages = [[NSMutableArray alloc] init];
[_messages addObjectsFromArray:_senderMsg];
NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages);

//Messages destiné à l'user
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error) {
if (!error) {
self.destinateurMsg = objects2;
[self.messages addObjectsFromArray:self.destinateurMsg];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];


NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
NSLog(@"EN DEHORS QUERY : %@", self.messages);
UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[exitButton addTarget:self action:@selector(backToInboxView) forControlEvents:UIControlEventTouchUpInside];
[exitButton setTitle:@"Inbox" forState:UIControlStateNormal];
exitButton.frame = CGRectMake(0.0, 0.0, 60, 60);
[self.view addSubview:exitButton];

}

最佳答案

您不能在这些行中访问 self.senderMsgself.messages

NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
NSLog(@"EN DEHORS QUERY : %@", self.messages);

因为它在异步请求完成之前执行。

[query1 findObjectsInBackgroundWithBlock:] 是一个在后台线程中执行的异步函数。调用这些函数后,viewDidLoad 中的执行将继续到函数结束。那时 NSLog 中的数组将为空。

如果您想使用数组的值,请在 findObjectsInBackgroundWithBlock 中使用它。

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// You can use it in this block.
// Reload Data here like [self.tableView reloadData]
}
}]

关于ios - 从 Parse 查询中访问 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27822310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com