在条件语句中转换 NSSEt
Casting NSSEt in an conditional statement
为什么无论索引 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;
}
让我们取消嵌套代码:
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];
为什么无论索引 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;
}
让我们取消嵌套代码:
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];