0

Here's my code:

NSMutableArray *ratings = [[NSMutableArray alloc] init];
    NSMutableDictionary *eachRating = [[NSMutableDictionary alloc] init];

    for (UIView *subview in self.rateServiceView.subviews) {

        if ([subview isKindOfClass:[RSTapRateView class]]) {

            RSTapRateView *rs = (RSTapRateView *)subview;
            [eachRating setObject:rs.rateId forKey:@"iRatingId"];
            [eachRating setObject:[NSNumber numberWithInt:rs.rating] forKey:@"iRate"];

            [ratings addObject:eachRating];

        }

    }

Instead of getting these values:

{
        iRate = 1;
        iRatingId = 1;
    },
        {
        iRate = 5;
        iRatingId = 2;
    },
        {
        iRate = 2;
        iRatingId = 3;
    }

I'm getting these values:

{
        iRate = 2;
        iRatingId = 3;
    },
        {
        iRate = 2;
        iRatingId = 3;
    },
        {
        iRate = 2;
        iRatingId = 3;
    }

When I logged the result for each iteration, the last object replaces all the existing objects and add a new object for itself.

3 Answers 3

2

move this line:

    NSMutableDictionary *eachRating = [[NSMutableDictionary alloc] init];

down to below this line:

    for (UIView *subview in self.rateServiceView.subviews) {

That way, you'll create a new "eachRating" dictionary which you'll add to your "ratings" array.

Sign up to request clarification or add additional context in comments.

1 Comment

great! it works fine now. Thank you so much! I'll keep this in mind :)
1

Yes this is because you are assigning different values to same key so new value replaces oldvalue for that key.

So change your code as :

  NSMutableArray *ratings = [[NSMutableArray alloc] init];
    for (UIView *subview in self.rateServiceView.subviews){

        if ([subview isKindOfClass:[RSTapRateView class]]) {

            NSMutableDictionary *eachRating = [[NSMutableDictionary alloc] init];


            RSTapRateView *rs = (RSTapRateView *)subview;
            [eachRating setObject:rs.rateId forKey:@"iRatingId"];
            [eachRating setObject:[NSNumber numberWithInt:rs.rating] forKey:@"iRate"];

            [ratings addObject:eachRating];

        }

    }

2 Comments

+1 to you Nishant, since you're really trying hard to earn that check mark. :-)
@MichaelDautermann initially I told the reason for the problem but then I thought to provide him the complete solution.
0

If you don't require further mutability of the individual dictionaries after this loop you can get a bit more modern and compact by writing it like this:

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

for (UIView *subview in self.rateServiceView.subviews) {

    if ([subview isKindOfClass:[RSTapRateView class]]) {

        RSTapRateView *rs = (RSTapRateView *)subview;
        [ratings addObject:@{@"iRatingId":rs.rateId, @"iRate":@(rs.rating)}];

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.