views:

443

answers:

3

HI all I have problem with

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

if I used NSString *filePaht = [[NSBundle mainBundle] pathForResource:@"soundName" ofType:@"aiff"]; it's OK

but when I used NSString *fileName = [[file.list objectAtIndex:index] objectForKey:@"soundName"]; NSString *filePaht = [[NSBundle mainBundle] pathForResource:fileName ofType:@"aiff"]; It's not work

have any idea !?

Thanks

+1  A: 

I am going to guess that fileName from file.list includes the file extension. So you are searching for "soundName.aiff.aiff" which does not exist. Try passing @"" for type or stripping the extension from fileName:

fileName = [fileName stringByDeletingPathExtension];
drawnonward
A: 

Check your Debugger Console, as it may be telling what you're doing wrong.

[file.list objectAtIndex:index]

If you're getting an NSRangeException, it may be because index contains an index that is outside the bounds of the array. Remember that arrays in Cocoa are serial, not associative; if you remove an object, the indexes of all the objects that came after it will go down by 1, upholding the invariant that 0 ≤ (every valid index) < (count of objects in the array).

It could also be because you never declared a variable named index.

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

If nothing is happening or you get an NSInternalInconsistencyException, it could be one of:

  • fileList is nil.
  • The dictionary returned from [file.list objectAtIndex:index] does not have an object for the key soundName.

If you got a “does not respond to selector” message in the Console, it may 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 that the class name you use when you declare the variable doesn't matter except to the compiler; at run time, it's just a variable holding a pointer to an object. The object can be of any class. It is perfectly valid to put something that isn't an NSString into an NSString * variable; it simply carries a very high (near certain) risk of wrong behavior and/or crashing shortly thereafter.

Such a crash will usually manifest in the form of a “does not respond to selector” exception (after something sends the object a message that NSString objects, for example, should respond to, but that the object doesn't respond to because it isn't an NSString).

Whichever problem you're having, you can use the Debugger to investigate.

Peter Hosey
A: 

Sorry with my fault. I get data from XML file and that data include "\n". yes I see "\n" so I replace with @"" but it not enough I must trim space value again.

Thanks for all advice ^_^

mr.octobor