I am trying to play some sound, that its name is a number . so i create an NSString as number, and when i try to set it to the NSBundle, it is Null!
It does work with a word, such as #"yes" , as the name parameter
//name parameter only works as a string in words. when its a number it doesn't.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
NSLog(#"%#",name); //logs 16 !
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; //crash!
the crash is because the soundFilePath is nil .
NSString *number=[NSString stringWithFormat:#"%d",16];
Ok. Problem is that i had to add the sounds into the build phase-copy bundle resources, so he can find them.
Related
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.
I'm implementing my drag&drop method. I need that when user drags something on my app window I can get that file URL. NSURL needs to be converted to char. Thats OK. But how to remove file:// from url? My current code:
pboard = [sender draggingPasteboard];
NSString *url = [[NSURL URLFromPasteboard:pboard] absoluteString];
input_imageN = strdup([url UTF8String]);
its OK, but it gives url with file:// prefix. I tried using
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
NSString *url = [fileUrl absoluteString];
NSLog(#"url: %#", [NSURL URLFromPasteboard:pboard]);
input_imageN = strdup([url UTF8String]);
but it says that
Cannot initialize a variable of type 'NSURL *' with an rvalue of type 'BOOL' (aka 'signed char')
at
NSURL *fileUrl = [[NSURL URLFromPasteboard:pboard] isFileURL];
To go from a file URL to the path as a C string in the appropriate representation for the filesystem, you'd do:
NSURL *fileURL = [NSURL URLFromPasteboard: pboard];
NSString *filePath = [fileURL path];
char *filesystemRepresentation = [filePath filesystemRepresentation];
This avoids assumptions that stripping off the scheme leaves you with just the path, or that the filesystem is definitely happy accepting UTF8-encoded paths.
url = [url stringByReplacingOccurencesOfString:#"file://" withString:#""];
Hope this helps. Cheers!
#user23743's answer is correct. Since iOS 7 though NSURL has its own filestSystemRepresentation method.
In Swift:
if let fileURL = NSURL(fromPasteboard: pboard) {
let representation = fileURL.fileSystemRepresentation
}
if let fileURL = NSURL(from: pboard)?.filePathURL {
}
has been most effective for me.
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.
data = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"INFO" ofType:#"plist"]];
name = [[data objectForKey:#"Name"]stringValue];
I am getting a SIGABRT error on when I try to give create name. All the names are alright. What could be wrong?
I have a INFO.plist file in my project. It has a row of type String. The value is Test.
Provided name is an NSString *, the following ought to work:
NSString *name = [data objectForKey:#"Name"];
NSDictionary's -objectForKey: returns the object, which will already be an NSString. (I'm not sure why you're calling -stringValue on it, but that could cause a crash or exception).
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.