【问题标题】:Add objects to NSMutableArray from another NSMutableArray (iOS)从另一个 NSMutableArray (iOS) 向 NSMutableArray 添加对象
【发布时间】:2016-08-08 16:41:10
【问题描述】:

我有一个像这样构造的NSMutableArray(ArrayOne)..

({
   "item_image_timeStamp" = "492364855.234597";
   "item_image_url" = "sample url";
   "item_image_vote" = 123;
}, {
   "item_image_timeStamp" = "492364458.236597";
   "item_image_url" = "sample url";
   "item_image_vote" = 456;
}, {
   "item_image_timeStamp" = "492364179.052397";
   "item_image_url" = "sample url";
   "item_image_vote" = 6184;
}, {
   "item_image_timeStamp" = "492364789.004447";
   "item_image_url" = "sample url";
   "item_image_vote" = 64;
}, {
   "item_image_timeStamp" = "492364356.002341";
   "item_image_url" = "sample url";
   "item_image_vote" = 3778;
})

ArrayOne 中最多可以包含 10 个对象。

然后,我有第二个 NSMutableArray(ArrayTwo) 结构就像 ArrayOne

({
   "item_image_timeStamp" = "492364855.234597";
   "item_image_url" = "sample url";
   "item_image_vote" = 123;
}, {
   "item_image_timeStamp" = "492364458.236597";
   "item_image_url" = "sample url";
   "item_image_vote" = 456;
})

..除了 ArrayTwo 中可以包含的最大对象数是 3。

现在,我想做的是..

  • 将 ArrayTwo 的对象添加到 ArrayOne 中(注意 ArrayOne 最多只能容纳 10 个对象)
  • 保留按“item_image_vote”键排序的前 5 个对象(不应替换前 5 个投票对象)
  • 如果需要替换 ArrayOne 中的任何对象,则应首先替换键“item_image_timeStamp”中具有最低值的项目。(最旧的对象将首先替换..然后是第二旧的对象)。

我希望我没有让我的问题变得太混乱。提前谢谢你。

【问题讨论】:

  • 你试过什么?此外,您的算法并不完全清楚 - 最终数组是否意味着包含 (a) First 中的原始前 5 个、(b) 所有 Second 和 (c) First 中最年轻的所有剩余项目?还是第二个中每个条目执行的算法,即从第二个添加第二个可能会从第二个中弹出刚刚添加的第一个?
  • 是的..正是你所说的..最终的数组旨在包含 (a) First 的原始前 5 名,(b) 所有 Second 和 (c) 最年轻的任何遗体First 中适合的项目

标签: objective-c nsmutablearray


【解决方案1】:

添加、删除和测量长度都是可变数组上的方法,可以直接应用。这个问题的有趣部分是排序标准,NSSortDescriptor 很好地解决了这个问题。

NSArray 允许将它们与sortedArrayUsingDescriptors 组合使用(注意复数形式)。所以一个好的方法是添加数组,根据你的标准排序并截断到最大长度。

// sort on vote, descending
NSSortDescriptor *voteDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"item_image_vote" ascending:NO];

// sort on date, descending
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"item_image_timeStamp" ascending:NO];

NSMutableArray *bigArray = [ArrayOne mutableCopy];  // note: lowercase variable names is considered preferable style
[bigArray addObjectsFromArray:ArrayTwo];

[bigArray sortUsingDescriptors:@[voteDescriptor, dateDescriptor]];
// note: order matters. you want votes sorted first, ties broken with date

结果是bigArray,现在已排序,截断到最大大小。

NSArray *result = [bigArray subarrayWithRange:NSMakeRange(0, MIN(10, bigArray.count))];

【讨论】:

  • 您正在删除投票率最低的最旧的,而不是投票数不是前 5 名的最旧的 - 这就是我认为的问题所指定的内容。但是问题中的算法并不精确,您可能已经实现了 OP 希望但没有正确描述的...
  • Sorry..this won't work..final array 旨在包含 (a) First 的原始前 5 个,(b) 所有 Second,以及 (c) 最年轻的所有剩余项目首先适合
【解决方案2】:

是的..正是您所说的..最终数组旨在包含 (a) First 的原始前 5 个,(b) 所有 Second,以及 (c) First 中最年轻的剩余项目适合

只需按步骤完成您的要求:

  1. 如果 First 和 Second 中的项目数合计

  2. "the original top 5 from First" - 按票数排序,您可以使用基于函数、块或描述符的排序。复制前 5 个(由于 [1] 必须至少有 5 个,因此此处不需要检查)作为您的结果。

  3. "all of Second" - 将 Second 的元素附加到结果中。现在,您的结果中有 6-8 项。

  4. 从 [2] 中复制已排序的 First 之后的第 6 项,并按年龄对结果数组进行排序。根据需要将前 2-4 项附加到您的结果中。

以上所有操作都可以使用标准的NSArray 排序、复制和附加方法完成,

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多