1

I am using below function to check whether if an object in an array is present in another array. If the object not present, then I will ADD that object to the new array, or else that object will NOT be included in the new array that I instantiated.

+ (NSMutableArray *)loadUngroupedSpeakerList
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSMutableArray *speakerList = [[NSMutableArray alloc] init];
    NSArray *speakerIDList = [userDefaults objectForKey:DATA_SPEAKER_IDLIST];
    NSArray *speakerIDListGrouped = [userDefaults objectForKey:DATA_SPEAKER_IDLIST_GROUPED];

    //**** this is for checking the contents of speakerIDListGrouped ****//
    for(NSString *speakerID in speakerIDListGrouped)
    {
        NSLog(@"FLOWCHECK~ loadUngroupedSpeakerList check content:%@", speakerID);
    }

    for(NSString *speakerID in speakerIDList)
    {
        if(![speakerIDListGrouped containsObject:speakerID])
        {
            NSLog(@"FLOWCHECK~ loadUngroupedSpeakerList: speakerID: %@", speakerID);

            NSDictionary *speakerDict = [userDefaults dictionaryForKey:[NSString stringWithFormat:@"%@%@", DATA_SPEAKER_DICT, speakerID]];

            [speakerList addObject:speakerDict];
        }
    }

    return speakerList;
}

In the above code, speakerList contains all the speakerIDs. While speakerIDListGrouped only contains the speakerIDs that are used in a group. My function needs to eliminate all the speakerIDs used in a group so I did it in a way just like the above code.

My Problem: When I run the code, I notice that even if speakerIDListGrouped contains the object in speakerIDList, these two lines would still be executed

NSDictionary *speakerDict = [userDefaults dictionaryForKey:[NSString stringWithFormat:@"%@%@", DATA_SPEAKER_DICT, speakerID]];

[speakerList addObject:speakerDict];

Whereas to I understand, It should not happen. Because I only allowed them to be executed only if speakerIDList does not contain that object.

This is the log when I execute the code:

2015-06-15 19:31:24.849 soulbeats[1936:433953] FLOWCHECK~ loadUngroupedSpeakerList check content:72243140485836704
2015-06-15 19:31:24.850 soulbeats[1936:433953] FLOWCHECK~ loadUngroupedSpeakerList check content:7782687177520836128
2015-06-15 19:31:24.850 soulbeats[1936:433953] FLOWCHECK~ loadUngroupedSpeakerList: speakerID: 72243140485836704
2015-06-15 19:31:24.851 soulbeats[1936:433953] FLOWCHECK~ loadUngroupedSpeakerList: speakerID: 7782687177520836128

As can be seen, speakerIDListGrouped DOES contain the two objects. However, when I tried replacing the string inside the lower for loop by hardcoding it to one of the objects I printed on Log, which was 72243140485836704. The function now works properly, I mean it didn't execute the two lines I showed before.

I am now confused. What is the difference between the string I hardcoded and the one that was obtained from the array? The contents are the same.

Many Thanks!

JLT
  • 3,052
  • 9
  • 39
  • 86
  • 1
    Seems that you want to know if a `NSString` inside a `NSDictionary` is present in `NSArray`, which doesn't do `containsObject:`. You could use `valueForKeyPath:` for instance giving you a `NSArray` of `speakerID`, and check wether it's inside or not. – Larme Jun 15 '15 at 12:12
  • Sounds like you want to use `NSSet`. – Droppy Jun 15 '15 at 12:13
  • 1
    Print the contents of `speakerIDListGrouped` and `speakerIDList`, also print the class of each object they contain - does everything match? – Wain Jun 15 '15 at 12:17
  • Hi @Larme, uhm no, the speakerIDList and speakerIDListGrouped contains NSStrings. Only the output array contains NSDictionary. – JLT Jun 15 '15 at 15:15
  • @Droppy I ll try that. Thanks! – JLT Jun 15 '15 at 15:15
  • @Wain The contents are the same. – JLT Jun 15 '15 at 15:15

2 Answers2

1

I did the same thing it is working fine...

NSMutableArray *speakerList = [[NSMutableArray alloc] init];
NSArray *speakerIDList = @[@"a",@"b",@"c",@"d",@"e"];
NSArray *speakerIDListGrouped =@[@"a",@"b",@"f",@"g",@"h"];
for(NSString *speakerID in speakerIDListGrouped)
{
    NSLog(@"%@", speakerID);
}

for(NSString *speakerID in speakerIDList)
{
    if(![speakerIDListGrouped containsObject:speakerID])
    {
        NSLog(@"FLOWCHECK~ loadUngroupedSpeakerList: speakerID: %@", speakerID);



        [speakerList addObject:speakerID];
    }
}

There might be some issue with the objects inside the array....

Akshay Agrawal
  • 217
  • 3
  • 12
  • For a very long time I noticed that it's my terrible mistake that leads to this problem. When I store the data in NSUserDefaults, it was NSNumber, that's why my code doesn't work. @Akshay Agrawal is right. There really is a issue with my objects in the array. – JLT May 25 '16 at 06:06
1

This answer will help other's. It's very simple, use following method of NSArray

id commonObject = [array1 firstObjectCommonWithArray:array2];

Ref: https://developer.apple.com/documentation/foundation/nsarray/1408825-firstobjectcommonwitharray?language=objc

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70