【问题标题】:Casting NSSEt in an conditional statement在条件语句中强制转换 NSSEt
【发布时间】:2020-09-05 01:15:01
【问题描述】:

为什么不管索引 0 处的对象是否在集合中,这句话总是正确的?

smallestNumberSort 是一个 NSMutableArray

smallestNumberSet 是一个 NSSet

if ([smallestNumberSet containsObject:[NSSet setWithObject:[smallestSortComplete objectAtIndex:0]]] == NO)
         [outcome removeAllObjects];

最小代码示例

-(void)GoodCoordinates
{
   NSCountedSet *outcome   = [[NSCountedSet alloc] init]; //will contain 4 random plot point between -50 and +50
   NSMutableArray smallestSortComplete = [NSMutableArray alloc] init];
   NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];


   NSMutableArray smallestCoords = [NSMutableArray alloc] initwithObjects: 
      [NSNumber numberWithInt:1]
      [NSNumber numberWithInt:7]
      [NSNumber numberWithInt:-14]
      [NSNumber numberWithInt:10]
      [NSNumber numberWithInt:-21],nil];

 BOOL (inBounds = NO)
   {   

     [smallestSortComplete removeAllObjects];
    smallestSortComplete = [NSMutableArray arrayWithArray:[outcome allObjects]]; 
    [smallestSortComplete sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
   
               
    if ([smallestCoords containsObject:[smallestSortComplete objectAtIndex:0]] == NO)
       [outcome removeAllObjects];
   else
      inBounds = YES;
}

【问题讨论】:

标签: xcode comparison nsset


【解决方案1】:

让我们取消嵌套代码:

id object = [smallestSortComplete objectAtIndex:0];
NSSet *set = [NSSet setWithObject:object];
if ([smallestNumberSet containsObject:set] == NO)
    [outcome removeAllObjects];

新创建的set 对象不在smallestNumberSet 中。测试object是否在smallestNumberSet中:

id object = [smallestSortComplete objectAtIndex:0];
if ([smallestNumberSet containsObject:object] == NO)
    [outcome removeAllObjects];

if ([smallestNumberSet containsObject:[smallestSortComplete objectAtIndex:0]] == NO)
    [outcome removeAllObjects];

【讨论】:

  • 在取消嵌套代码后运行这两个测试,它们都失败了。我敢肯定这是在盯着我看,但让我发疯了。
  • @KevinMcFadden 请在问题中发布minimal reproducible example
猜你喜欢
  • 2012-04-12
  • 1970-01-01
  • 2011-11-02
  • 2012-05-27
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多