gpt4 book ai didi

iphone - 比较两个数组并在数组中取回公共(public)值

转载 作者:行者123 更新时间:2023-11-28 22:31:49 24 4
gpt4 key购买 nike

为了比较这两个数组,我使用了 NSMutableSet,然后将这两个集合相交以获得数组中的共同结果。NSMutableSet *set1 = [NSMutableSet setWithArray:array];

[set1 intersectSet:[NSSet setWithObject:[NSNumber numberWithInt:70]]];

NSArray *intersectArray = [[NSArray alloc]init];
intersectArray =[set1 allObjects];
NSLog(@"the testing array is %@",[intersectArray objectAtIndex:0])];

它给了我完美的答案,但是当 set1 没有与 set2 相同的元素时它会崩溃。 intersectArray 为空。如何为 intersectArray 获取 nil 值。

最佳答案

尝试使用:

 if ([set1 intersectSet:[NSSet setWithObject:[NSNumber numberWithInt:70]]])

{
NSArray *intersectArray = [[NSArray alloc]init];
intersectArray =[set1 allObjects];
NSLog(@"the testing array is %@",[intersectArray objectAtIndex:0])];
{

关于iphone - 比较两个数组并在数组中取回公共(public)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com