Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

objective c - 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];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...