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
];
Use a mutable dictionary, then you can add in the extra fields
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
You cannot set nil object to key (ie
[params setObject:nil forKey:@"key]will crash)