xcode - 条件文で NSSEt をキャストする

okwaves2024-01-25  9

インデックス 0 のオブジェクトがセット内にあるかどうかに関係なく、このステートメントが常に true になるのはなぜですか?

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;
}

[smallestSortComplete objectAtIndex:0] が smallestCoords にある、再現可能な最小限の例を投稿してください。

– ヴィレケ

2020 年 9 月 9 日 10:51



------------------------

コードのネストを解除しましょう:

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

新しく作成されたセット オブジェクトは smallestNumberSet にありません。オブジェクトが smallestNumberSet にあるかどうかをテストします:

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

または

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

2

コードのネストを解除した後に両方のテストを実行しましたが、どちらも失敗しました。何かが私の顔を見つめているのは確かですが、私は気が狂いそうになります。

– ケビン・マクファーデン

2020 年 9 月 5 日 22:13

@KevinMcFadden 質問に再現可能な最小限の例を投稿してください。

– ヴィレケ

2020 年 9 月 7 日、8:31

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。