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

  • Home
  • SEARCH
  • 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 7128035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:05:46+00:00 2026-05-28T11:05:46+00:00

I have an NSDictionary with two values. Now I want to switch the values.

  • 0

I have an NSDictionary with two values. Now I want to switch the values. Here is my code:

NSDictionary *aDictionary = [[NSDictionary alloc] init];
[aDictionary setValue:@"theValue1" forKey:@"theKey1"];
[aDictionary setValue:@"theValue2" forKey:@"theKey2"];

NSString *tmpValue = [aDictionary objectForKey:@"theKey1"];
[aDictionary setValue:[myDict objectForKey:@"theKey2"] forKey:@"theKey1"];
[aDictionary setValue:tmpValue forKey:[myDict objectForKey:@"theKey2"]];
NSLog(@"%@", myDict); 


//output
theKey1 = theValue2;
theKey2 = theValue2;
theValue2 = theValue1;

What am I doing wrong?

  • 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-05-28T11:05:46+00:00Added an answer on May 28, 2026 at 11:05 am

    Other than a couple of larger problems (you should be using NSMutableDictionary instead of NSDictionary if you want to change things, and you should use the setObject:forKey: method instead of the setValue:forKey: method), the root of your problem is this line:

    [aDictionary setValue:tmpValue forKey:[myDict objectForKey:@"theKey2"]];
    

    Think about what key you’re setting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@one, @oneKey, @two, @twoKey,
I have a problem in the following two code lines NSDictionary* last = [[arrHistory
In my app, I have a dictionary with two array like: NSDictionary *d=[[NSDictionary alloc]initWithObjectsAndKeys:listOfEvents,@l,arr_distance,@d,
I have a dependent UIPicker that loads the values into its two components from
I want to merge two NSDictionary in one NSDictionary. my first DIctionary BreakFast Dates
I have two NSArray variables, each one of then gets a similar NSDictionary inside.
i want to have two data on each row on my tableview. Something like
I have two problems. 1- I am using following code to add background music
Using the Graph Facebook API with this code [facebook requestWithGraphPath:@me/friends andDelegate:self]; Now I have
Using the below code, the issue is I have two buttons and two textfields

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.