How to append a variable to a string in xcode - objective-c

Im trying to to append a variable to a string but its showing an error. Im missing something ridiculously easy here but my mind is shot.
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSURL *urla = [NSURL URLWithString:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=",eID];

You can't concat 2 string variables just by putting a comma between them. Try this instead:
NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#",eID]];

If all you are doing is appending, you have several options:
A. Use NSString & stringWithFormat:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *urlString = [NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#", eID];
NSURL *urla = [NSURL URLWithString:urlString];
B. Use NSString & stringByAppendingString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSString *urlString = [baseUrl stringByAppendingString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
C. Use NSMutableString & appendString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSMutableString *urlString = [NSMutableString stringWithString:baseUrl];
[urlString appendString:eID];
NSURL *urla = [NSURL URLWithString:urlString];

Try like this:-
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *url=#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];

Related

replace in NSURL

How can I do a string replace in NSURL?
I tried stringByReplacingOccurrencesOfString but it works with NSString.
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
I want to replace image url
http://example.com/image1.jpg
to
http://example.com/img1.jpg
NSURL has an absoluteString method that you could use like so
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
You could also directly operate on the NSString from the dataList as well:
NSString *urlString = [dataList[indexPath.item][#"image"] stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
NSString *webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
imageURL = [NSURL URLWithString:webStringURL];
Hope This will help you.
You are going to replace the last path component of the URL so use the dedicated API of NSURL:
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSURL *newURL = [[imageURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"img.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL: newURL];
stringByReplacingOccurrencesOfString could cause unexpected behavior.

Is there an easy way to shorten URL's with parameters in Objective C using goo.gl?

This does not seem to be working:
NSString *key = #"mykey";
NSString *shortenedStrURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://www.googleapis.com/urlshortener/v1/url?key=%#&longUrl=%#", key,strURL]] encoding:NSUTF8StringEncoding error:nil];
return [NSURL URLWithString:strURL];

NSURLRequest with url containing special characters

I'm having the same problem as:
NSURL with special characters
But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://suggestqueries.google.com/complete/search?output=firefox&q=%#", encodedString];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
This works:
http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)
Any ideas?
EDIT:
Using the debugger the NSURL looks correct:
string __NSCFString * #"åäö" 0x0a77cd60
encodedString __NSCFString * #"%C3%A5%C3%A4%C3%B6" 0x0a77fc40
url NSURL * #"http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0
Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.
Instead of using
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
try using
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];

NSURL returns Nil Value

Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.
Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];

Append NSString to NSURL?

I have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL.
Thanks.
I think it's good solution:
NSURL *bUrl = [aUrl URLByAppendingPathComponent:#"newString"];
In Swift you could do the following,
var bURL = aURL.URLByAppendingPathComponent( "newString" )
You can also state whether the URL is a directory,
var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )
I think it's as simple as:
NSString *s = [aUrl.path stringByAppendingString:#"newString"];
If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];
You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;