8

I am using NSMutableArray. I want to fetch the values by date like we do in SQL group by "log_date".

logMuArray (
        {
        "log_currenttime" = "4:30pm";
        "log_date" = "11.12.2011";
        "log_duration" = "1:30";
    },
        {
        "log_currenttime" = "4:33pm";
        "log_date" = "11.12.2011";
        "log_duration" = "2:21";
    },
        {
        "log_currenttime" = "4:40pm";
        "log_date" = "11.12.2011";
        "log_duration" = "5:30";
    },
        {
        "log_currenttime" = "7:30pm";
        "log_date" = "12.12.2011";
        "log_duration" = "1:30";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "12.12.2011";
        "log_duration" = "2:21";
    },
        {
        "log_currenttime" = "7:40pm";
        "log_date" = "12.12.2011";
        "log_duration" = "5:30";
    },
        {
        "log_currenttime" = "07:16pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:07";
    },
        {
        "log_currenttime" = "7:31pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:04";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:03";
    },
        {
        "log_currenttime" = "7:33pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:06";
    },
        {
        "log_currenttime" = "7:35pm";
        "log_date" = "19.12.2011";
        "log_duration" = "0:05";
    }
)

**So, I have just performed....

 NSLog(@"logMuArray %@",[logMuArray valueForKey:@"log_date"]);

But I want to fetch the UNIQUE dates only.** I have thought about NSPredicate or Mutable Set etc...

logMuArray (
    "11.12.2011",
    "11.12.2011",
    "11.12.2011",
    "12.12.2011",
    "12.12.2011",
    "12.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011",
    "19.12.2011"
)

Thanks in advance.....

EDIT:

I have also heared about "@distinctUnionOfObjects"

......

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57
  • Is it possible to uniquely identify objects based on two or more keys with same logic? – Ajay Gabani Jul 08 '15 at 04:10
  • Your question is good, better you should ask a question, because code is also required, so please ask your question in stack overflow. Side by side I have not much idea upon this, but I need a time to solve it. Will try to solve it, but need a code for solving it. Better you post your question at stack overflow with code. thanks – Arpit B Parekh Jul 08 '15 at 06:54
  • it might help you [Rebuild an NSArray by grouping objects w.r.t any matching keys in each dictionary in that array](http://stackoverflow.com/questions/15375528/rebuild-an-nsarray-by-grouping-objects-that-have-matching-id-numbers/38472661#38472661) – umakanta Jul 20 '16 at 05:03

5 Answers5

15

Shanti's answer is close. You want to use the Key-Value Coding collection operator @distinctUnionOfObjects. Place the operator immediately preceding the key which you want it to affect, as if it is a part of the key path you are accessing:

[logMuArray valueForKeyPath:@"@distinctUnionOfObjects.log_date"]

Notice the use of valueForKeyPath:, not valueForKey: The former is a method in the Key-Value Coding protocol, and allows accessing arbitrary depth of attributes. The key path is an NSString made up of dot-separated keys. The result of each key lookup is used in turn to access the next key (starting with the original receiver); by default, valueForKey: is simply called at each step.

jscs
  • 63,694
  • 13
  • 151
  • 195
3

You can use KVC for this.

[logMuArray valueForKey:@"@distinctUnionOfArrays.log_date"]

edit: Editing this wrt Josh's Response

[logMuArray valueForKeyPath:@"@distinctUnionOfArrays.log_date"]
Shanti K
  • 2,873
  • 1
  • 16
  • 31
  • [logMuArray valueForKeyPath:@"@ distinctUnionOfArrays.log_date"], change valueForKey with valueForKeyPath – Raj Jun 20 '12 at 12:34
3

You should use NSSet for UNIQUE items like :

NSSet *filteredData = [NSSet setWithArray:[logMuArray valueForKey:@"log_date"]];
Maulik
  • 19,348
  • 14
  • 82
  • 137
1

Try this logic might help you

-(NSMutableArray *) makeUnique :(NSMutableArray *) array {    
    int i;
    int count = [array count];
    for (i =0; i< count ; i++) {
        NSRange range = NSMakeRange (i+1, count); 
        [array removeObject:[array objectAtIndex:i] inRange:range];
    }
    return array;
}
Nakkeeran
  • 15,296
  • 1
  • 21
  • 22
0

You can incorporate a set

Here is some example code

NSMutableArray * mArray = [NSMutableArray array];
NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys:@"foo",@"bar",@"bar",@"oooo",nil];
NSDictionary *d2 = [NSDictionary dictionaryWithObjectsAndKeys:@"boo",@"bar",@"bar",@"oooo",nil];
NSDictionary *d3 = [NSDictionary dictionaryWithObjectsAndKeys:@"boo",@"bar",@"bar",@"oooo",nil];
[mArray addObject:d1];
[mArray addObject:d2];
[mArray addObject:d3];
NSLog(@"the array\n%@", mArray);
NSLog(@"just bar %@", [mArray valueForKey:@"bar"]);
//get unique values
NSSet * set = [NSSet setWithArray:[mArray valueForKey:@"bar"]];
NSLog(@"unique just bar %@", [set allObjects]);

and here is the output

2011-12-20 01:50:59.034 TestEnvironment[32401:207] the array
(
        {
        bar = foo;
        oooo = bar;
    },
        {
        bar = boo;
        oooo = bar;
    },
        {
        bar = boo;
        oooo = bar;
    }
)
2011-12-20 01:50:59.036 TestEnvironment[32401:207] just bar (
    foo,
    boo,
    boo
)
2011-12-20 01:50:59.038 TestEnvironment[32401:207] unique just bar (
    foo,
    boo
)
Jesse Black
  • 7,966
  • 3
  • 34
  • 45