【问题标题】:So I've stored an NSDictionary object in an NSMutableArray, how do I know use the NSDictionary keys to grab these objects out of the array?所以我在 NSMutableArray 中存储了一个 NSDictionary 对象,我怎么知道使用 NSDictionary 键从数组中抓取这些对象?
【发布时间】:2014-05-08 22:55:44
【问题描述】:

我有两个UISlider 代表物品的最低和最高价格。我将这些以及其他各种数据传递回之前的控制器。

我使用了一种协议方法并将前一个控制器设置为委托,以便将值传递回控制器。

我可以轻松地从数组中抓取其他对象,因为它们是字符串。我只是使用:

     [_finalSelectionForRefinement containsObject:@"size"];

这是我在推送控制器中所做的:

      // create dictionary with keys minimum and maximum that hold the 
      // position of the slider as a float  

      _dictionaryWithSliderValues = 
      [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat:_minSliderPosition], @"minimum",
      [NSNumber numberWithFloat:_maxSliderPosition], @"maximum", nil];

      // store this in the array that is retrieved in previous controller
      [_finalSelectionForRefinement addObject:_dictionaryWithSliderValues];

我的问题是我现在如何使用最小和最大键来抓取滑块位置浮动对象?

以为我可以使用 NSPredicate,但我在博客和 YouTube 上遇到的示例对我的特定需求没有帮助。

希望能得到一些帮助

问候

更新 - 前一个控制器中我需要检索滑块最小值和最大值的方法的简短片段:

-(PFQuery *)queryForCollection
{
    PFQuery *query = [PFQuery queryWithClassName:@"Garments"];

        if (_selectedRowInFilterPicker == 0) {
            NSLog(@"ORDER BY RECOMMENDED");
            [query orderByDescending:@"recommended"];
        } else if (_selectedRowInFilterPicker == 1) {
            NSLog(@"ORDER BY NEWEST ITEMS");
            [query orderByDescending:@"createdAt"];
        } else if (_selectedRowInFilterPicker == 2) {
            NSLog(@"ORDER BY DESCENDING USING PRICE");
            [query orderByDescending:@"price"];
        } else if (_selectedRowInFilterPicker == 3) {
            NSLog(@"ORDER BY ASCENDING USING PRICE");
            [query orderByAscending:@"price"];
        }


        if ([_selectionFromRefineResultsController count] > 0) {
            NSLog(@"Selection FROM REF MADE");

            // Gender
            if ([_selectionFromRefineResultsController containsObject:@"Male"]) {
                [query whereKey:@"gender" equalTo:@1];
            }
            if ([_selectionFromRefineResultsController containsObject:@"Female"]) {
                [query whereKey:@"gender" equalTo:@2];
            }
        }

        // Here I need to check there is a minimum or maximum value in the array
        // If there is I can user [query whereKey:@"price" greaterThan:MINVAL] and MAXVAL
        // This will return items within the correct price range.

这个 queryForCollection 方法是从另一个名为 performQuery 的方法中调用的,该方法在第二个控制器的按钮被点击以将数据传递回首先推送它的控制器时调用。

【问题讨论】:

  • 在需要使用这些值的地方发布您的代码。除了实际进行过滤的细节之外,您应该进行所有设置。
  • 有什么理由不能将 _finalSelectionForRefinement 设为字典吗? _finalSelectionForRefinement 中除了 _dictionaryWithSliderValues 之外还有其他字典吗?
  • @rmaddy 该代码将毫无用处,因为它只是一个对我的数组进行多次调用的方法,例如[_finalSelectionForRefinement containsObject:@"colour"];, [_finalSelectionForRefinement containsObject:@"size"];等等。因为我正在使用的这个特定对象不是字符串,所以我无法通过这种方式搜索它。
  • Ethan 我没有将 _finalSelectionForRefinement 设为 NSDictionary,因为我存储在其中的 90% 的对象都是可以用来识别每个对象的字符串。使用 NSMutableArray 更有意义。字典会起作用,但不会。嗯,这将是最后的手段,因为我必须重构一大堆代码。
  • @LondonGuy 我不确定我是否理解你在做什么......如果你没有与该字符串关联的其他值,检查数组是否包含 @"size" 有什么好处?

标签: objective-c cocoa-touch nsmutablearray nsdictionary nspredicate


【解决方案1】:

您应该查看 NSMutableArrayNSArrayNSDictionary 的文档

这将解释每个实例方法。

但简而言之,您添加的任何对象都应该在 NSDictionary 中,因此它具有值和键。这包括您的任何字符串。这样做可以简化您使用键进行搜索的方式。

如果 NSMutableArray 包含不是 KVC 的对象,那么我认为你会发现它更难一次扫描对象。

由于 NSmutableArray 继承自 NSArray,因此您可以在其对象值或对象对象值具有键的 NSmutableArray 上使用实例方法 valueForKey:

valueForKey:

返回一个包含调用结果的数组 valueForKey:对数组的每个对象使用键。

  • (id)valueForKey:(NSString *)key

粗略的例子:

  NSMutableArray * finalSelectionForRefinement =[[NSMutableArray alloc]init];
  NSDictionary *dictionaryWithSliderValues = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat:10], @"minimum", [NSNumber numberWithFloat:20], @"maximum", nil];
    
     NSDictionary *stringValues = [[NSDictionary alloc] initWithObjectsAndKeys:@"the-size", @"size", @"the-hight", @"hight", nil];
    
  
    [finalSelectionForRefinement addObject:dictionaryWithSliderValues];
     [finalSelectionForRefinement addObject:stringValues];
    
    
    
    
    NSLog(@"finalSelectionForRefinement = %@", [finalSelectionForRefinement valueForKey:@"maximum"] );

首先,您可以将所有内容存储在一个更有意义的 NSDictionary 中。但我想向您展示 valueForKey: 将在每个范围内进行搜索。

另一件事是 valueForKey: 将返回一个包含结果的 NSArray。它发现的任何与您要查找的键不匹配的对象都将作为 NSNull 对象返回。即

finalSelectionForRefinement = (
    20,
"<null>"
)

因此,您仍然需要单独列出您的价值。一种方法是像这样使用 objectEnumerator

 NSEnumerator *enumerator = [[finalSelectionForRefinement valueForKey:@"maximum"] objectEnumerator];
    id anObject;
    
    while (anObject = [enumerator nextObject]) {
       
        if(![anObject  isKindOfClass:[NSNull class]])
         {
          NSLog(@"anObject = %@", anObject);
        }
       
    }

应该返回:

 anObject = 20

很可能有更好的方法来做到这一点。以上所有只是给你一个想法。而且我怀疑你可以通过使用绑定来删减很多代码。

(另请注意,此答案是在您提问更新之前构建的)

【讨论】:

  • 这让我来回答。我将 NSMutablearray 转换为 NSMutabledictionary。将所有内容存储为 NSMutableDictionaries。使用 valueForKey 检索。使用 objectForKey 调用存储在 var 中的“floatValue”检索浮点 NSNumber 对象。然后检查的值大于 0。如果这意味着滑块被移动,那么必须有一个值。在我的查询中添加了一个或多个值。完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
相关资源
最近更新 更多