我正在使用 Parse.com 制作一个 iOS 应用程序,我想知道是否有办法检索类中的总行数?例如,我有一个包含 100 个对象的“MESSAGE”类,有没有办法检索该整数,例如:
int x = [计数消息];//现在x = 100;
一旦我的变量 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]
}];
将它们放在一起,你可以这样做:
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]
}];
}];
关于ios - 计算类中的行数并选择给定数字的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28725082/