简体   繁体   中英

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 ie 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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM