stringByReplacingOccurrencesOfString not working? - objective-c

The first NSLog returns the correct URL After that I get this error.
2013-07-06 21:07:56.622 Social App[69682:14003] -[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x9a4ae10
videoURL = [[tableData objectAtIndex:indexPath.row] objectForKey:#"streamURL"];
NSLog(#"URL; %#", videoURL);
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"http://www.youtube.com/watch?v=" withString:#""];
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"&feature=youtube_gdata_player" withString:#""];
NSLog(#"URL; %#", videoURL);
Any help is greatly appreciated.

On the third line it seems you are sending a NSString message to a NSURL. That is why the second NSLog says [NSURL stringBy...] and unrecognized selector, since the NSURL class does not have a method named stringBy....
What you have to do is insert this line between the second and third lines:
NSString* videoString = [videoURL absoluteString];
And then substitute videoURL with videoString on the lines where you call stringBy....

Use videoURL.absoluteString.
Right now you are trying to use a NSString method ( stringByReplacingOccurrencesOfString ) on a NSURL, that of course won't work. absoluteString will get you a NSString to work with.

videoURL is a NSURL
use:
stringUrl = [videoURL absoluteString];
then use stringurl for further operations like stringByReplacingOccurrencesOfString

Related

Too many arguments to method call ,expected 1 have 2

I'm calling a JSON URL from the NSURL class. My URL is:
like://Design_Time_Addresses/IServices/RoleService/json/Role/?name=%#",[textField text]];
When I write "doctor" in the textfield I have to get all the details of doctor through that URL. Similarly when I write "engineer" I have to get all the details of engineer. So it means what I type in the textfield, the name in URL should be replaced with the textfield value. But when I write the code like this:
NSURL *jsonUrl =[NSURL URLWithString:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSString *jsonStr =[[NSString alloc] initWithContentsOfURL:jsonUrl];
NSMutableDictionary *jsonDetails = [[NSMutableDictionary alloc]initWithDictionary:[jsonStr JSONValue]];
As shown here I am using the NSURL class to get this URL.
I am getting an error in the NSURL class. How do I pass the text fields value to a URL?
First line should be
NSString *urlString = [NSString stringWithFormat:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSURL URLWithString expects a single string argument, while you are providing two. Since you probably want to construct a single string from the #"http://..." string and [textField text], you should use an NSString method to concatenate them. You can't rely on NSURL to concatenate the string for you.

passing a string var to a function

When I run the code below
NSString *chemin;
chemin = [NSString stringWithFormat:#"chapitre0%d", [sender tag]];
NSLog(#"chemin : %#",chemin);
[detailChapitre loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:chemin ofType:#"html"]isDirectory:NO]]];
I got :
'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'
but *chemin is initialized to the right value in the log.
Certainly a noob question,
Thx for your help
This could be it, the chemin variable might have the correct chapter but the path might be wrong. So when you pass the wrong filepath to NSURL it would have returned null which inturn would have caused the crash.
First get the absolute path like so -
NSString *path = [[NSBundle mainBundle] pathForResource:#"chapter1" ofType:#"html"];
try initializing chemin and then release it. like this:
NSString *chemin;
chemin = [[NSString alloc] initWithFormat:#"chapitre0%d", [sender tag]];
NSLog(#"chemin : %#",chemin);
[detailChapitre loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:chemin ofType:#"html"]isDirectory:NO]]];
[chemin release];
This would seem to indicate that the file "chapitre0[whatever].html" is not where you expect it to be. This could be because an incorrect string is being constructed, but it could also just mean your build process isn't sticking the file in the right place or something like that. Look at the string indicated in your NSLog and then look in your built apps's Resources directory to confirm that the file is indeed there and named correctly.

NSURL is null in Simulator, but okay on iPad

I'm trying to load an audio file into AVAudioPlayer on the iPad. When I run it on the iPad it finds it in the bundle fine. However, if I try and run it through the simulator, I get a null error for NSURL. Here's the snippet of code (num is an arbitrary int):
NSString *name = [NSString stringWithFormat:#"st-answermachine-%i", num];
NSLog(#"name = %#", name);
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"m4a"];
NSLog(#"path = %#", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(#"url = %#", url);
In the simluator, the Debugger Console traces this:
name = st-answermachine-1
path = /Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a
url = (null)
But if I try it on the device, I get this:
name = st-answermachine-1
path = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a
url = /var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a
Any ideas why I might have this problem please?
Thanks!
URLWithString: expects a string containing an actual URL as its parameter (e.g. 'http://blah/' or 'file:///blah'). URLs can't contain spaces (as the simulator's path does), and that's why it's failing.
As Evan suggests, you need to use fileURLWithPath: to convert a path string to a URL object.

Problem in converting urlString into NSURL in iphone sdk

I am having string at first the method calls with timestamp value nil and I am getting converted the string into url .next time when I click load more results button again the method calls with time stamp value assigned to it.but the url string is not converting into NSURL iam getting the null value into it.
-(NSMutableArray*)getTextMessagesArray:(NSString *)endTimestamp
{
printf("\n endtimestamp value...%s",[endTimestamp UTF8String]);
NSString *urlString = #"http://123.237.186.221:8080/upload/textRequest.jsp";
urlString = [urlString stringByAppendingString:#"?beginTimestamp="];
urlString = [urlString stringByAppendingString:#"&endTimestamp="];
if([endTimestamp length]>0)
{
urlString = [urlString stringByAppendingString:endTimestamp];
}
printf("\n &*(*(((urlString...%s",[urlString UTF8String]);
NSURL* aUrl = [NSURL URLWithString:urlString];
NSLog(#"url in appdelegaare in text...%#",aUrl);
[textParser parseXMLFileAtURL:aUrl];
textMessagesList = [textParser getTextMessagesList];
printf("\n textMessagesList Count in appDelegate....%d",[textMessagesList count]);
return textMessagesList;
}
The result I am getting in console is:
&*(*(((urlString...http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08 16:20:47.0
url in appdelegaare in text...(null)
Guy's can any one suggest me why this happening
Anyone's help will be much appreciated.
Thanks to all,
Monish.
Your problem is that valid URLs cannot contains spaces. You want to do something along the following lines:
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// escapedUrlString should be "http://123.237.186.221:8080/upload/textRequest.jsp?endTimestamp=2010-10-08%2016:20:47.0"
NSURL *aUrl = [NSURL URLWithString:escapedUrlString];
This might be what you wanted.

How to download file from particular url?

NSURL * url = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSData * data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
NSLog(#"\nis not nil");
NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
I write this code to download a file from given url... but i get an error on line
NSData * data = [NSData dataWithContentsOfURL:url];
uncaught exception...
so please help me out.
Your first line should be
NSURL * url = [NSURL URLWithString:#"http://192.168.100.161/UploadWhiteB/wh.txt"];
(NSURL is not a string, but can easily be constructed from one.)
I'd expect you to get a compiler warning on your first line--ignoring compiler warnings is bad. The second line fails because dataWithContentsOfURL: expects to be given a pointer to an NSURL object and while you're passing it a pointer that you've typed NSURL*, url is actually pointing to an NSString object.
NSString *file = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSURL *fileURL = [NSURL URLWithString:file];
NSLog(#"qqqqq.....%#",fileURL);
NSData *fileData = [[NSData alloc] initWithContentsOfURL:fileURL];
-[NSString initWithContentsOfURL:] is deprecated. You should be using -[NSString (id)initWithContentsOfURL:encoding:error:]. In either case, the URL paramter is an NSURL instance, not an NSData instance. Of course you get an error trying to initialize a string with the wrong type. You can initialize the string with the URL data using -[NSString initWithData:encoding:], or just initialize the string directly from the URL.