0

To compare the two array I used NSMutableSet and then intersect the two sets to get the common result in an array. 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])];

It gives me perfect answer but it get crashed when set1 does not have common element as that of set2. intersectArray is empty . How to get nil value for intersectArray.

GameBegins
  • 580
  • 7
  • 12
  • Use the `count` property to check if array has any items. – A-Live Jun 19 '13 at 15:29
  • 1
    You just can't ask an empty array for its first item, so just the log is wrong. Also, don't create a new empty array `[[NSArray alloc]init];` and then throw it away on the next line by setting a different value - very wasteful... – Wain Jun 19 '13 at 15:39
  • I tried count property but its giving me warning (Compoarision between pointer and integer (NSUIntefer (aka "unasigned int") and void)) – GameBegins Jun 19 '13 at 17:23

2 Answers2

1

2 ways to overcome this problem.

1) If no common number then set1 is empty. Thus, before allocating NSArray check if set1 has atleast one element.

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

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

2) When you want to get an element of an array then before getting element check if array is not empty and it has an element at index you want to get.

[set1 intersectSet:[NSSet setWithObject:[NSNumber numberWithInt:70]]];
NSArray *intersectArray = [[NSArray alloc]init];
intersectArray = [set1 allObjects];

if(intersectArray.count && intersectArray.count > indexOfElement) {
    NSLog(@"the testing array is %@",[intersectArray objectAtIndex:0])];
}
Geek
  • 8,280
  • 17
  • 73
  • 137
  • I already tried it if ([arrray count]==nil) { NSLog(@"the array is empty"); } ... the code solved my purpose but I'm getting a warning (Compoarision between pointer and integer (NSUIntefer (aka "unasigned int") and void)) – GameBegins Jun 19 '13 at 17:15
  • Even it tried this method for (int i=0; i<[nanpCodeArray count]; i++) { // // NSString *st = [nanpCodeArray objectAtIndex:i]; // // NSLog(@"the number is %@",st); // if ([areaCodeString isEqualToString:st]) { // NSLog(@"area code matches with nanparry %@",[nanpCodeArray objectAtIndex:i]); // //break; // } // } but even though two strings become equal I'm not getting NSLogStatement. I guess there is an issue with if statement but I don't know what – GameBegins Jun 19 '13 at 17:19
  • Don't compare [array count] with nil. [array count] returns an NSUInteger, so compare it with 0. `if ([arrray count]==0)` – Idles Jun 19 '13 at 17:21
  • @user2041198 See my code to look at the way I check array count. count cannot be nil. Why don't you just copy paste my code. – Geek Jun 19 '13 at 17:24
  • Thanks Akash, It works fine but instead of [set1 intersectSet:[NSSet setWithObject:[NSNumber numberWithInt:70]]]; code if I pass a NSString value [set1 intersectSet:[NSSet setWithObject:someString]]; even though two string matched it gives me nill array count – GameBegins Jun 19 '13 at 18:08
  • @user2041198 Try inserting a string in an array and then use it. [set intersectSet:[NSSet setWithArray:[NSArray arrayWithObject:yourString]]]; – Geek Jun 20 '13 at 04:05
1

try to use:

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

{
NSArray *intersectArray = [[NSArray alloc]init];
intersectArray =[set1 allObjects];
NSLog(@"the testing array is %@",[intersectArray objectAtIndex:0])];
{
TonyMkenu
  • 7,597
  • 3
  • 27
  • 49