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.

3
  • Use the count property to check if array has any items. Commented Jun 19, 2013 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... Commented Jun 19, 2013 at 15:39
  • I tried count property but its giving me warning (Compoarision between pointer and integer (NSUIntefer (aka "unasigned int") and void)) Commented Jun 19, 2013 at 17:23

2 Answers 2

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])];
{
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

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))
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
Don't compare [array count] with nil. [array count] returns an NSUInteger, so compare it with 0. if ([arrray count]==0)
@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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.