0

So I have a NSString with a url like this:

NSString stringWithFormat:@"/reading.php?title=blah&description="blah"&image_url=blah... "

what is the best way to append query string to this string? is there a dictionary kind of way to do this?

1
  • Could you rephrase and be more specific with what you want? Commented Mar 20, 2012 at 22:48

4 Answers 4

2

What you want to do is this.

[NSString stringWithFormat:@"/reading.php?title=blah&description=%@&image_url=blah... ",blah];
  • Basically %@ in the context meaning that you'll pass use a dynamic value which will be a string.
Sign up to request clarification or add additional context in comments.

3 Comments

yea, but it's too long and it's not clean & pretty if I have 20 parameters in the query string
Then you shall divide your 20 parameters into another strings and then add it into your main string.
There is no "pretty" way to add 20 dynamic values to a string
1

How about a category?

This is not great but for a first pass should give you something to get started

@interface NSDictionary (ps_additions)

- (NSString *)ps_URLParamsValue;

@end

@implementation NSDictionary (ps_additions)

- (NSString *)ps_URLParamsValue;
{
    NSMutableString *params = [NSMutableString string];

    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
        [params appendFormat:@"%@=%@&", key, obj];
    }];

    return [params copy];
}

@end

This would end up with something like:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"42", @"special_number", @"value", @"another", nil];

NSString *myString = [NSString stringWithFormat:@"/reading.php?%@", [params URLParamsValue]];

NSLog(@"%@", myString);

#=> 2012-03-20 23:54:55.855 Untitled[39469:707] /reading.php?another=value&special_number=42&

2 Comments

When done right, category methods almost always make things prettier. I agree that the dictionary category above is a good idea. I added an answer below that could easily be a string category method. One little issue on your soln here is that I think it will leave a dangling ampersand at the end of the param string.
I don't think the dangling ampersand would cause any issues as it would most likely just be ignored, but this was a first pass and would hopefully be improved if it was going to be used.
0

You can use something like:

NSString *parameter1 = @"blah";
NSString *parameter2 = @"anotherblah";
NSString *fullURL = [NSString stringWithFormat:@"/reading.php?title=%@&image_url=%@", parameter1, parameter2];

You can add as many parameters as you want. Use "%@" where you will be dynamically adding the text.

Good luck :)

Comments

0

Copy pasting from Paul.s - which is the correct answer, imo - and fixing a (most likely inconsequential) problem of a dangling ampersand...

@interface NSDictionary (ps_additions)

- (NSString *)ps_URLParamsValue;

@end

@implementation NSDictionary (ps_additions)

- (NSString *)ps_URLParamsValue;
{
    if (!self.count) return @"";

    NSMutableString *params = [NSMutableString string];

    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
        [params appendFormat:@"%@=%@&", key, obj];
    }];

    // return everything except that last ampersand
    return [[params copy] substringToIndex:[params length]-1];
}

@end

5 Comments

As @Paul.s correctly suggests above, a category method will clean up the code a bit. You can package this one on NSString by simply removing the to:urlString parameter and appending the query string to self. Also, watch out for that special case on the last iteration - you don't need an extra "&" on the end of the query string. (though most web services might find it harmless).
You could move the lastObject call outside of the loop as it's not going to change.
agreed. also, i'm not certain, but i bet your enumeration block is more efficient because it just walks the dictionary data structure picking the keys and values in a single pass. no hash needed (probably).
And come to think of it, I think the dictionary category is more generally useful than the string category idea. The only value-add to string is the append. Not sure what the correct SO thing to do here is. I guess I'll up-vote your answer? or edit mine? I'll do both.
Ah now you see why I didn't bother trying to deal with the ampersand issue in a clean way, it's entirely possible that the dictionary would be empty in which case this would crash with an NSRangeException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.