4

I need to replace "CurrentLocation" with a value in following URL. "CurrentLocation" is a predefined string and it gives a dynamic value (I mean different name of location/city each time). I need help, How to do it in Objective-C ??

NSURL *MainURL = [NSURL URLWithString:@"http://news.google.com/news?q=location:CurrentLocation&output=rss"];

In JavaScript i would do something like this;

var a="Google"; var b=".com"; var c=".np"; var d=a+b+c; document.write(d);

Please somebody help me with this. Thanks!!

2 Answers 2

12

You can use the the stringWithFormat static message on the NSString class. This will create an autoreleased NSString object using the formatting you specify (using printf style format string). So for your case:

// get the current location from somewhere
NSString * yourCurrentLocation = @"Sydney";

// create your urlString %@ is replaced with the yourCurrentLocation argument
NSString * urlString = [NSString stringWithFormat:@"http://news.google.com/news?q=location:%@&output=rss", yourCurrentLocation];
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your quick response. I tried as you described, but sad thing is It says "too many arguments to method call, expected 1, have 2." :(
@Himalay You must have made a mistake copying this into your code. I did a verbatum copy and paste into my project and these lines compile. Try setting yourCurrentLocation explicitly (edited answer).
One more thing, how can i call a NSString value of "- (void)" into a "- (BOOL)" ?
@Himalay Glad you sorted it out. Not sure what you mean by that last comment, perhaps write it up as a new question with more information :).
@Himalay If you are happy with this answer, you can click the "accepted answer" tick to mark it as such ... welcome to StackOverflow.
1

also, a good method is stringByAppendingString, here is more info in the apple site.

NSString *errorTag = @"Error: ";
NSString *errorString = @"premature end of file.";
NSString *errorMessage = [errorTag stringByAppendingString:errorString];

Comments

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.