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
a source to share
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 keysoundName
.
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.
a source to share