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 7031223
In Process

The Archive Base Latest Questions

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

The code below shows the way I am preparing the params to be submitted

  • 0

The code below shows the way I am preparing the params to be submitted to the server. I have a if-else clause to check and determine what params to submit i.e. if question id exists, submit only question id, else submit question text and topic id.

However, I feel that this is a little repetitive and not the best form of coding (in my POV). How can I refactor the code to make it more efficient (remove duplicate)

if(self.question._id || [self.question._id isEqual:[NSNull null]])
{
    //Set up params for submission
    NSDictionary *params= [[NSDictionary alloc] initWithObjectsAndKeys:
                          self.answer.text, @"text",
                          //if question id exists, submit question id only 
                          self.question._id, @"question_id", 
                          shareFb, @"fbShare", 
                          shareTwitter, @"twitterShare",
                          self.question.source,@"questionSource",
                          self.answer.videoId,@"youtube_id",
                          nil
                          ];
}
else
{
    //Set up params for submission
    NSDictionary *params= [[NSDictionary alloc] initWithObjectsAndKeys:
                          self.answer.text, @"text", 
                          //else submit question text and topic id
                          self.question.text, @"question_text",
                          self.question.topic._id, @"topic_id", 
                          shareFb, @"fbShare", 
                          shareTwitter, @"twitterShare",
                          self.question.source,@"questionSource",
                          self.answer.videoId,@"youtube_id",
                          nil
                          ];
  • 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-28T00:46:30+00:00Added an answer on May 28, 2026 at 12:46 am

    Use a mutable dictionary, then you can add in the extra fields

    NSMutableDictionary *params= [[NSMutableDictionary alloc] initWithObjectsAndKeys:
            self.answer.text, @"text",
            //if question id exists, submit question id only 
            self.question._id, @"question_id", 
            shareFb, @"fbShare", 
            shareTwitter, @"twitterShare",
            self.question.source,@"questionSource",
            self.answer.videoId,@"youtube_id",
            nil
            ];
    
    if(!(self.question._id == nil || [self.question._id isEqual:[NSNull null]]))
    {
        [params setObject:self.question.text forKey:@"question_text"];
        [params setObject:self.question.topic._id forKey:@"topic_id"];
    }
    

    EDIT IN RESPONSE TO COMMENT 1

    If any of the params being set in the first statement above might be nil, you will need to be more careful

    NSMutableDictionary *params= [[NSMutableDictionary alloc] init];
    if (self.answer.text) {
        [params setObject:self.answer.text forKey:@"text"];
    }
    •
    •  // all the params between self.answer.text and self.answer.videoId
    •
    if (self.answer.videoId) {
        [params setObject:self.answer.videoId forKey:@"youtube_id"];
    }
    if(!(self.question._id == nil || [self.question._id isEqual:[NSNull null]]))
        {
            if(self.question.text) {
                [params setObject:self.question.text forKey:@"question_text"];
            }
            if (self.question.topic._id) {
                [params setObject:self.question.topic._id forKey:@"topic_id"];
            }
        }
    }
    

    You cannot set nil object to key (ie [params setObject:nil forKey:@"key] will crash)

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

Sidebar

Related Questions

I have the code below that hides and shows the navigational bar. It is
I have the code below which now shows a standard windows help popup where
The code below shows a sample that I've used recently to explain the different
The code below shows me (I think) that the for each loop is about
My PHP code below shows an image on browser as its original size 300x300.
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with
I have noticed that my geocoder is inconsistent in the code shown below because
I have a python psp page code is shown below. Currently it only prints
The code below shows nested while loops, but it's not the very efficient. Suppose
The code below shows a line in different colors, but that's a lot of

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.