Ios 计算类中的行数,并为给定的数字选择一行
我正在使用Parse.com制作一个iOS应用程序,我想知道是否有方法检索类中的总行数?例如,我有一个包含100个对象的“MESSAGE”类,是否有一种方法可以检索该整数,如: int x=[count MESSAGE]//现在x=100Ios 计算类中的行数,并为给定的数字选择一行,ios,objective-c,mongodb,parse-platform,database,Ios,Objective C,Mongodb,Parse Platform,Database,我正在使用Parse.com制作一个iOS应用程序,我想知道是否有方法检索类中的总行数?例如,我有一个包含100个对象的“MESSAGE”类,是否有一种方法可以检索该整数,如: int x=[count MESSAGE]//现在x=100 一旦变量x中有100,我将得到一个介于1和100之间的随机数,假设函数返回7,有没有办法检索第7行中的对象?您可以像这样获取计数: PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"]; [query
一旦变量x中有100,我将得到一个介于1和100之间的随机数,假设函数返回7,有没有办法检索第7行中的对象?您可以像这样获取计数:
PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
// count tells you how many objects matched the query
}];
PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"];
// Skip the first 6, retrieve the next 1
query.skip = 6;
query.limit = 1;
[query findObjectsInBackgroundWithBlock:^(NSArray *messages, NSError *error) {
// Now you have the 7th MESSAGE at messages[0]
}];
您可以得到第7个对象,如下所示:
PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
// count tells you how many objects matched the query
}];
PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"];
// Skip the first 6, retrieve the next 1
query.skip = 6;
query.limit = 1;
[query findObjectsInBackgroundWithBlock:^(NSArray *messages, NSError *error) {
// Now you have the 7th MESSAGE at messages[0]
}];
把它们放在一起,你可以做:
PFQuery *query = [PFQuery queryWithClassName:@"MESSAGE"];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
// Skip the first <random>, retrieve the next 1
query.skip = arc4random_uniform(count);
query.limit = 1;
[query findObjectsInBackgroundWithBlock:^(NSArray *messages, NSError *error) {
// Now you have a random MESSAGE at messages[0]
}];
}];
PFQuery*query=[pfqueryqueryquerywithclassname:@“MESSAGE”];
[query countObjectsInBackgroundWithBlock:^(整数计数,N错误*错误){
//跳过第一个,检索下一个1
query.skip=arc4random_uniform(计数);
query.limit=1;
[查询findObjectsInBackgroundWithBlock:^(NSArray*消息,NSError*错误){
//现在,在消息[0]处有一条随机消息
}];
}];
哇,这很快,也很容易理解。似乎是个不错的主意,让我试试,我会让你知道的,非常感谢伊恩!!回答得很好。也可以执行GetFirstObjectInBackgroundithBlock而不是.limit=1。