pathForResource 不起作用

发布于 2024-08-31 10:38:55 字数 529 浏览 11 评论 0原文

我有问题,

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

如果我用过的话

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

就OK了。但是当我使用它时

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

它不起作用

有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城凉梦凉人心 2024-09-07 10:38:55

我猜测 file.list 中的 fileName 包含文件扩展名。所以您正在搜索不存在的“soundName.aiff.aiff”。尝试传递 @"" 作为类型或从文件名中删除扩展名:

fileName = [fileName stringByDeletingPathExtension];

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];
金兰素衣 2024-09-07 10:38:55

检查您的调试器控制台,因为它可能会告诉您做错了什么。

[file.list objectAtIndex:index]

如果您收到 NSRangeException,可能是因为 index 包含超出数组范围的索引。请记住,Cocoa 中的数组是串行的,而不是关联的;如果删除一个对象,则该对象之后的所有对象的索引都会减 1,从而保持 0 ≤ (每个有效索引) < 的不变量。 (数组中对象的计数)。

也可能是因为 您从未声明过名为 index 的变量。

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

如果没有发生任何事情或者您收到 NSInternalInconsistencyException,则可能是以下之一:

  • fileList is nil
  • [file.list objectAtIndex:index] 返回的字典没有键 soundName 的对象。

如果您在控制台中收到“不响应选择器”消息,则可能是以下之一:

  • file.list 是一个对象,但不是 NSArray。
  • [file.list objectAtIndex:index] 不是 NSDictionary。
  • fileName ([[file.list objectAtIndex:index] objectForKey:@"soundName"]) 不是 NSString。

请记住,声明变量时使用的类名除了对编译器而言并不重要;在运行时,它只是一个保存指向对象的指针的变量。该对象可以属于任何类。将非 NSString 的内容放入 NSString * 变量中是完全有效的;它只是带来非常高(几乎肯定)的错误行为和/或此后不久崩溃的风险。

这种崩溃通常会以“不响应选择器”异常的形式出现(例如,在某些东西向对象发送一条消息后,NSString 对象应该响应,但该对象没有响应,因为它不是不是 NSString)。

无论您遇到哪种问题,都可以使用调试器 进行调查。

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.

揽清风入怀 2024-09-07 10:38:55

对不起我的错。
我从 XML 文件获取数据
并且该数据包括“\n”。是的,我看到“\n”,所以我用@“”替换
但这还不够,我必须再次修剪空间值。

谢谢大家的建议^_^

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 ^_^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文