Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:46:29+00:00 2026-06-06T02:46:29+00:00

I have an issue that (I think) might have to do with scope, but

  • 0

I have an issue that (I think) might have to do with scope, but I’m not sure. I’m trying to do something that I think should be simple, but I am getting a strange result, and I could truly use some advice. I would say I’m an early-objective-c programmer, but not a complete newb.

I have written a function in objective-c that I would like to use to change the key-names in a mutable array of mutable dictionary objects. So, I want to pass in a mutable array of mutable dictionary objects, and return the same mutable array with the same dictionary objects, but with some of the key-names changed. Make sense?

I have tried several log statements in this code, which seem to indicate that everything I’m doing is working, except when the for loop is finished executing (when I try to test the values in the temp array), the array appears to contain only the LAST element in the source array, repeated [source count] times. Normally, this would lead me to believe I’m not writing the new values correctly, or not reading them correctly, or even that my NSLog statements aren’t showing me what I think they are. But might this be because of scope? Does the array not retain its changes outside of the for loop?

I have put a fair amount of time into this function, and I have exhausted my bag of tricks. Can anyone help out?

-(NSMutableArray *)renameKeysIn:(NSMutableArray*)source {
/*
// Pre:
// The source array is an array of dictionary items.
// This method renames some of the keys in the dictionary elements, to make sorting easier later. 
// - "source" is input, method returns a mutable array
 */

// copy of the source array
NSMutableArray *temp = [source mutableCopy];

// a temporary dictionary object:
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];

// These arrays are the old field names and the new names
NSMutableArray *originalField = [NSMutableArray arrayWithObjects:@"text", @"created_at",nil];
NSMutableArray *replacedField = [NSMutableArray arrayWithObjects:@"title", @"pubDate", nil];

// loop through the whole array
for (int x =0; x<[temp count]; x++) {
    // set the temp dictionary to current element
    [dict setDictionary:[temp objectAtIndex:x]]; 

    // loop through the number of keys (fields) we want to replace (created_at, text)... defined in the "originalField" array 
    for (int i=0; i<[originalField count]; i++)
    {   
            // look through the NSDictionary item (fields in the key list)
            // if a key name in the dictionary matches one of the ones to be replaced, then replace it with the new one
            if ([dict objectForKey:[originalField objectAtIndex:i]] != nil) {
                // add a new key/val pair: the new key *name*, and the old key *value* 
                [dict setObject:[dict objectForKey:[originalField objectAtIndex:i]] 
                         forKey:[replacedField objectAtIndex:i]];
                // remove the old key/value pair
                [dict removeObjectForKey:[originalField objectAtIndex:i]];
            }// end if dictionary item not null

    }// end loop through keys (created_at, text)

    [temp replaceObjectAtIndex:x withObject:dict];

}// end loop through array

// check array contents
for (int a=0; a<[temp count]; a++){
    NSLog(@"Temp contents: ############ %@",[[temp objectAtIndex:a] objectForKey:@"pubDate"]);
}

return temp;    
} // end METHOD
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:46:30+00:00Added an answer on June 6, 2026 at 2:46 am

    I think the issue is on the line with:

    [dict setDictionary:[temp objectAtIndex:x]];
    

    Since these things are almost all working in pointers (instead of copying contents), every element of your temp array will point to the dict dictionary, which is set to be whatever the latest key’s dictionary is. I think setting the actual pointer will fix the issue.

    dict = [temp objectAtIndex:x];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty strange issue, but I think it might be that I
Having an issue here that I have tried everything I can think of but
I have a strange issue that has arisen recently: Whenever I enter text, even
I'm a beginner in Objective-c and i have a little issue that i'm sure
Okay, I think I might be over-complicating this issue but I truly am stuck.
So, I´ve got an issue that someone might have solved (or so I hope).
Greetings! I have an issue here that i can't find. I am getting a
I have an issue with a site I oversee: http://www.aifittings.com that I have not
I have a rather complicated issue that I am trying to solve in PHP
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.