Edward Tattsyrup
Edward Tattsyrup

Reputation: 208

Need help in my code - I am getting incorrect data using NSArray

I have a for-loop that iterates over an NSArray to extract the data but my data is duplicating itself.

My initial NSArray 'timeProfiles', contains 24 NSArrays, each of which contain 16 objects. For each array in timeProfiles I am trying to add that data to 'timescale object'.

I've tried adding each timescaleInfo object into an array but still, I get depleted values.

I've also tried incrementing the first index of timeProfiles like this timeProfiles[z+1] etc, but nothing has worked so far..

A push in the right direction would be greatly appreciated.

Code:

for (int z = 0; z < timeProfiles.count; z+= 12) {


    for (int i = 0; i < 12; i++) {//Time slices (12)


  tP = [timeProfiles objectAtIndex:z + i];
    //0SFTWTMS
    //00000SSH
    //Setting time profiles


        NSLog(@"timeprofile  %@", tP);


     timeScaleInfo0 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[0][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[0][3] timeScaleEndTime:timeProfiles[0][4] weekdayFlag:weekDayFlag0 clendarFlag:@"00000000"];
     timeScaleInfo1 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[1][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[1][3] timeScaleEndTime:timeProfiles[1][4] weekdayFlag:weekDayFlag1 clendarFlag:@"00000000"];
     timeScaleInfo2 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[2][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[2][3] timeScaleEndTime:timeProfiles[2][4] weekdayFlag:weekDayFlag2 clendarFlag:@"00000000"];
     timeScaleInfo3 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[3][2] integerValue]  timeSliceID:[[tP objectAtIndex:15]integerValue] timeScaleStartTime:timeProfiles[3][3] timeScaleEndTime:timeProfiles[3][4] weekdayFlag:weekDayFlag3 clendarFlag:@"00000000"];
     timeScaleInfo4 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[4][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[4][3] timeScaleEndTime:timeProfiles[4][4] weekdayFlag:weekDayFlag4 clendarFlag:@"00000000"];
     timeScaleInfo5 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[5][2] integerValue]  timeSliceID:[[tP objectAtIndex:15]integerValue] timeScaleStartTime:timeProfiles[5][3] timeScaleEndTime:timeProfiles[5][4] weekdayFlag:weekDayFlag5 clendarFlag:@"00000000"];
     timeScaleInfo6 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[6][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[6][3] timeScaleEndTime:timeProfiles[6][4] weekdayFlag:weekDayFlag6 clendarFlag:@"00000000"];
     timeScaleInfo7 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[7][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[7][3] timeScaleEndTime:timeProfiles[7][4] weekdayFlag:weekDayFlag7 clendarFlag:@"00000000"];
     timeScaleInfo8 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[8][2] integerValue]  timeSliceID:[[tP objectAtIndex:15]integerValue] timeScaleStartTime:timeProfiles[8][3] timeScaleEndTime:timeProfiles[8][4] weekdayFlag:weekDayFlag8 clendarFlag:@"00000000"];
     timeScaleInfo9 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[9][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[9][3] timeScaleEndTime:timeProfiles[9][4] weekdayFlag:weekDayFlag9 clendarFlag:@"00000000"];
     timeScaleInfo10 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[10][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[10][3] timeScaleEndTime:timeProfiles[10][4] weekdayFlag:weekDayFlag10 clendarFlag:@"00000000"];
     timeScaleInfo11 = [[TimeScaleInfo alloc] initWithTimeScaleID:[timeProfiles[11][2] integerValue]  timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:timeProfiles[11][3] timeScaleEndTime:timeProfiles[11][4] weekdayFlag:weekDayFlag11 clendarFlag:@"00000000"];
    }

    TimeSliceInfo *timesliceInfo = [[TimeSliceInfo alloc] initWithTimeSliceID:[[tP objectAtIndex:15] integerValue]  timeScaleInfoArray:@[timeScaleInfo0, timeScaleInfo1, timeScaleInfo2, timeScaleInfo3, timeScaleInfo4, timeScaleInfo5, timeScaleInfo6, timeScaleInfo7, timeScaleInfo8, timeScaleInfo9, timeScaleInfo10, timeScaleInfo11]];



    [timeSliceArray addObject:timesliceInfo];

    userKeyInfo.timeSliceInfoArray = timeSliceArray;

}

Upvotes: 0

Views: 63

Answers (2)

Edward Tattsyrup
Edward Tattsyrup

Reputation: 208

Took a while to figure out, but here is the working solution.

 NSMutableArray *timeSliceArray =[[NSMutableArray alloc] init];

    for (int z = 0; z < timeProfiles.count; z+=12) { //increments every 12 arrays then extracts data.

        NSMutableArray *timeScaleInfoArray = [[NSMutableArray alloc] init];
        NSArray *tP;
        for (int i = 0; i < 12; i++) {//Time slices (12)

            //00000SSH

            tP = [timeProfiles objectAtIndex:z + i];
            NSString *weekDayFlag = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",@"0",tP[10],tP[9],tP[8],tP[7],tP[6],tP[5],tP[11]];
            NSString *calendarFlag = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@",@"0",@"0",@"0",@"0",@"0",tP[12], tP[13],tP[14]];

            TimeScaleInfo *timeScaleInfo = [[TimeScaleInfo alloc] initWithTimeScaleID:[[tP objectAtIndex:2] integerValue] timeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleStartTime:tP[3] timeScaleEndTime:tP[4] weekdayFlag:weekDayFlag clendarFlag:calendarFlag];

            [timeScaleInfoArray addObject:timeScaleInfo];
        }

        TimeSliceInfo *timesliceInfo = [[TimeSliceInfo alloc] initWithTimeSliceID:[[tP objectAtIndex:15] integerValue] timeScaleInfoArray:timeScaleInfoArray];

        [timeSliceArray addObject:timesliceInfo];
    }

    userKeyInfo.timeSliceInfoArray = timeSliceArray;

Upvotes: 1

DevesH
DevesH

Reputation: 508

Here - for (int z = 0; z < timeProfiles.count; z+= 12)

Is z = z + 12 that what you want?. I think it should be z = z + 1

Please correct me if I am missing something.

Upvotes: 1

Related Questions