ios - 计算类中的行数并选择给定数字的行

标签 ios objective-c mongodb parse-platform database

我正在使用 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/

相关文章:

ios - 圆形,3 色渐变,动画进度 View

node.js - 如何让heroku使用DB的环境配置

objective-c - Objective C block 语法 - Xcode 自动完成功能不起作用

ios - 在 Objective-C 的 Viewcontroller 类之外的 uiwebview Controller 上调用函数

json - 执行正确的 MongoDB 查询(类型错误 : Converting circular structure to JSON)

如果属性数组不包含对象,MongoDB 会查找文档

iphone - 如何释放单例?

ios - 包含 UIScrollView 的缩放后添加到 UIImageView 的注释不在正确的位置

ios - UIActivityIndi​​catorView 不会显示在 UIWebView 中

ios - UITextField 的 NSLayoutConstraints - 错误