Question

i am constructing All data to look like a response data dictionary from a server. Now, newsFeedsDict1 which should Dictionary for both Bolly and Global is not only showing all data inside Global dictionary only. While my for loop is running its showing correct data for Bolly. but for 2nd time its showing Bolly's data also in Global dictionary.

if(internetStatus == NotReachable) {

    NSMutableArray *titleArr = [[NSMutableArray alloc] init];
    NSMutableArray *wholeFeeds = [[[NSMutableArray alloc] init] autorelease];

    [titleArr addObject:@"Bolly"];
    [titleArr addObject:@"Global"];

    for (NSString *title in titleArr) {



    //titleArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"TitleArray"];

    NSLog(@"TITle arr %@",titleArr);

    NSLog(@"No internet");
    OrderedDictionary *newsFeedsDict1 = [[[OrderedDictionary alloc] init] autorelease];

    NSMutableDictionary *newsFeedsDict = [[[NSMutableDictionary alloc] init] autorelease];

    NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];

    myLocalArray = [[Database sharedDatabase] getArticleData:title];

    NSMutableDictionary *articleDict = [[[NSMutableDictionary alloc] init] autorelease];

    [articleDict setObject:myLocalArray forKey:@"article"];

    [newsFeedsDict setObject:articleDict forKey:@"Articles"];
        [newsFeedsDict setObject:title forKey:@"@name"];

    [newsFeedsDict1 setObject:newsFeedsDict forKey:title];

        [wholeFeeds addObject:newsFeedsDict1];

    NSLog(@"news feed dict %@",newsFeedsDict1);

    NSMutableDictionary *temparticleDictionary = [[NSMutableDictionary alloc] init];
   self.articleDictionary = temparticleDictionary;


    self.categoriesDictionary = [[NSMutableDictionary alloc] init];
                    self.categoriesDictionary =newsFeedsDict1;


            [self createArticleDictionaryForCategory:newsFeedsDict];


        }


}
Was it helpful?

Solution

It's difficult to tell what your code is supposed to do, and how you can tell that one dictionary has the same content as another. A couple problems:

NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];

The first line is entirely unnecessary. You're creating a new array, assigning it to myLocalArray, and then assigning something else to myLocalArray. You do the same later with self.categoriesDictionary. This leads me to believe that you have some misunderstanding with respect to object pointers.

So, the array that you get from your shared database ends up at myLocalArray, and you then add that array to the dictionary at articleDict, and then add articleDict to newsFeedDict and in turn add that to newsFeedDict1. And then you log newsFeedDict1. You do exactly the same for both your titles, @"Bolly" and @"Global", so it's not at all surprising that you see the same output in both cases.

I'm having a hard time seeing why you expect the output to be different, and I have to guess that again it's due to a misunderstanding of what happens when you assign one object pointer to another. Or, perhaps you're expecting the array that you get from [[Database sharedDatabase] getArticleData:title]; to be different because you're passing in different titles. Maybe you really should be getting different arrays there; it would be a good idea to look at what happens in that -getArticleData: method and whether it really is giving you the right results for each title.

If that doesn't help, take some time to clean up your code so that it's easier for us, and more importantly, for you to really see what's going on. Also, please edit your question to give a better description of what you're seeing and how that's different from what you expect to see.

OTHER TIPS

Can you write the snippet for *getArticleData()* method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top