hacker
hacker

Reputation: 8947

Check if the value of key in an array of objects is same iOS?

I'd an NSArray with some dictionaries.which has some keys and values.which I'm showing in a tableview. I'd put the section header title is one of the value of a key in that dictionary i.e. like ddDate value. The problem is the same ddDate values needs to come under the same section. So I need to create my array according to that.

responseDict (
        {
        xx = 16;
        yy = 17;
        aa = 0;
        bb = 6;
        cc = 2016;
       ddDate = "17-06-2016";
    },
        {
        xx = 17;
        yy = 17;
        aa = 0;
        bb = 6;
        cc = 2016;

        ddDate = "17-06-2016";

    },
        {
        xx = 18;
        yy = 17;
        aa = 0;
        bb = 6;
        cc = 2016;

        ddDate = "18-06-2016";

    }

ie date is the section title. And those who had the same ddDate values under one section. Can anybody guide me how I can achieve this?

Upvotes: 0

Views: 987

Answers (2)

Paulw11
Paulw11

Reputation: 114865

Your goal is to build two data structures; An array of ddDate values; this will be your section information and a dictionary of arrays, keyed by the ddDate values; this will be your row data.

@property (strong, nonatomic) NSArray *sections;
@property (strong, nonatomic) NSDictionary *rows;

- (void)processJSONData:(NSArray *)inputArray {

    NSMutableDictionary *extractedData = [NSMutableDictionary new];
    for (NSDictionary *dict in inputArray) {
        NSString *ddDate = dict["ddDate"];
        NSMutableArray *rowArray = extractedData[ddDate];
        if (rowArray == nil) {
            rowArray = [NSMutableArray new];
            extractedData[ddDate] = rowArray;
        }
        [rowArray addObject:dict];
    }

    self.sections = [extractedData allKeys];
    self.rows = [NSDictionary dictionaryWithDictionary:extractedData];
}

You might like to sort the self.sections array.

The number of sections is self.sections.count and the number of rows in a section are self.rows[self.sections[indexPath.section]].count

Upvotes: 1

gurmandeep
gurmandeep

Reputation: 1247

You will have to create custom Arrays then compare dates, and if the date is same then add it under one header title else move it to the next header title. As such i don't see a shortcut out here.

Upvotes: 0

Related Questions