PathForResource not working

I have a problem with

NSString *filePaht = [[NSBundle mainBundle] pathForResource:(NSString *)name ofType:(NSString *)ext];

      

if i used

NSString *filePaht = [[NSBundle mainBundle] pathForResource:@"soundName" ofType:@"aiff"];

      

OK. but when i used

NSString *fileName = [[file.list objectAtIndex:index] objectForKey:@"soundName"];
NSString *filePaht = [[NSBundle mainBundle] pathForResource:fileName ofType:@"aiff"];

      

Does not work

I have an idea!?

thanks

+2


a source to share


3 answers


I'm going to guess that fileName from file.list includes the file extension. So you're looking for "soundName.aiff.aiff", which isn't there. Try passing @ "" to type or remove an extension from fileName:



fileName = [fileName stringByDeletingPathExtension];

      

+6


a source


Check the debugger console as this may tell you what you are doing wrong.

[file.list objectAtIndex:index]

      

If you get an NSRangeException, it might be because it index

contains an index that is outside the array. Remember that arrays in Cocoa are serial, not associative; if you delete an object, the indices of all objects that come after it will decrease by 1, maintaining the invariant that 0 ≀ (each valid index) <(number of objects in the array).

It could also be because you have never declared a variable with a nameindex

.

NSString *fileName = [[file.list objectAtIndex:index] objectForKey:@"soundName"];
NSString *filePaht = [[NSBundle mainBundle] pathForResource:fileName ofType:@"aiff"];

      

If nothing happens or you get an NSInternalInconsistencyException, it can be one of the following:



  • fileList

    - nil

    .
  • The dictionary returned from [file.list objectAtIndex:index]

    does not have an object for the key soundName

    .

If you receive a "not responding to selector" message in the console, it could be one of:

  • file.list

    is an object, but not an NSArray.
  • [file.list objectAtIndex:index]

    is not an NSDictionary.
  • fileName

    ( [[file.list objectAtIndex:index] objectForKey:@"soundName"]

    ) is not an NSString.

Remember, the class name you use when declaring a variable is irrelevant, except for the compiler; at runtime, it is just a variable containing a pointer to an object. The object can be of any class. It is perfectly correct to put something that is not an NSString into a variable NSString *

; it simply carries a very high (almost definite) risk of misbehaving and / or failure shortly thereafter.

This crash usually manifests itself as a "not responding to selector" exception (after something sends a message to the object, to which, for example, NSString objects should respond, but that the object does not respond because it is not an NSString).

Whatever problem you are having, you can use the debugger to investigate.

+1


a source


Sorry for your mistake. I am getting data from an XML file and this data includes "\ n". yes, I see "\ n", so I'll replace with @ "" but that's not enough, I have to trim the space value again.

Thanks for all the tips ^ _ ^

0


a source







All Articles