在我的 mainBundle 中找不到 .js 文件?

发布于 2024-10-09 12:25:10 字数 545 浏览 0 评论 0原文

这让我发疯,因为我无法弄清楚世界上到底发生了什么。我一直从主包加载文件,xml 文件,html 文件等。但是,现在我试图获取 javascript 文件的内容,但它永远找不到它。我正在使用:

NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];
    if (jsData) {
        NSLog(@"%@", jsData);
    } else {
        NSLog(@"Can't find file");
        return;
    }

即使检查 [[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"] 字符串也会返回 null。

我的 globaly.js 文件位于我的 Resources 文件夹中,这是我的其他文件所在的位置,使用上述方法完全可以正常工作。

为什么找不到我的js文件?

This is driving me crazy as I cannot figure out what in the world is going on. I load up files form you main bundle all the time, xml files, html files, etc. But, now I am trying to get the contents of a javascript file but it can never find it. I am using:

NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];
    if (jsData) {
        NSLog(@"%@", jsData);
    } else {
        NSLog(@"Can't find file");
        return;
    }

Even checking the [[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"] string returns null.

My globaly.js file is in my Resources folder, the exact location where my other files are location that work totally fine using the above method.

Why can't it find my js file?

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

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

发布评论

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

评论(5

你另情深 2024-10-16 12:25:10

默认情况下,Xcode 可能会将带有“js”扩展名的文件视为源文件,并将它们添加到其编译源构建阶段而不是其复制捆绑资源构建阶段。

只需将您的“js”文件移至复制捆绑资源构建阶段,它们就会被复制到您的应用程序的捆绑中。它们是否属于项目层次结构中名为“资源”的组并不重要;这只是一个组织工具,它在 Xcode 中没有任何意义。目标下的构建阶段实际上完成了产品的组装工作。

By default Xcode may be treating files with the "js" extension as source files, and adding them to its Compile Sources build phase rather than its Copy Bundle Resources build phase.

Just move your "js" files to the Copy Bundle Resources build phase and they'll be copied into your application's bundle. Whether they're in a group named Resources in the project hierarchy is immaterial; that's just an organizational tool, it has no meaning within Xcode. It's the build phases under the targets that actually do the work of putting together a product.

醉梦枕江山 2024-10-16 12:25:10

据我所知,Xcode 不喜欢带有 *.js 扩展名的文件。我的意思是它没有将其添加到应用程序包中。只需将文件重命名为 *.txt 即可。

正确的答案是:您需要将 js 文件从编译源目标构建阶段移动到复制捆绑资源。因为Xcode认为js文件是源文件,应该编译它们然后将其添加为资源。

As far I remembered Xcode doesn't like files with *.js extension. I mean it didn't add it into application bundle. Just rename your file to *.txt and all will be ok.

And right answer will be: Your need to move your js files from Compile Sources target build phase to Copy Bundle Resources. Because Xcode thinks that js files are source files and they should be compiled instead then added as resources.

稍尽春風 2024-10-16 12:25:10

这并不是说 Xcode 不喜欢 .js 文件。它确实考虑了它们,否则我们不会在 webview 中使用 .js 文件来执行 javascript。

只是我们需要在targets下的Copy Bundle资源构建阶段添加.js文件。只需将 .js 文件拖到复制包资源构建阶段即可。因此 Xcode 将考虑该文件,因为它现在位于 bulid 阶段包中。
我认为如果我们不执行上述步骤,我们会在构建代码时收到警告。我不太记得这个警告,但它只是说 Xcode 无法识别 .js 文件。

将文件添加到构建阶段后,您可以使用
NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];

It is not that the Xcode dont't like .js files. It does consider them else we would have not used .js files for javascriptexecution in webview.

It is just that we need to add the .js file in Copy Bundle resource build phase under targets. Just drag your .js file into the copy bundle resource build phase. Thus Xcode will consider this file since it is now in the bulid phase bundle.
I think we if we don't do the above step, we get a warning on building the code. I don't exactly remember the warning but it just said that Xcode cannot recognize .js file.

Once the file is added to the build phase, you can use
NSData *jsData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"global" ofType:@"js"]];

许久 2024-10-16 12:25:10

我遇到了同样的问题。

我执行了以下步骤来解决此问题:

  1. 将 js 文件定位到构建设置中的“编译源”部分,并将其从那里删除。

  2. 找到“复制捆绑资源”部分并在其中添加 js 文件。

以上2个步骤解决了我的问题..

I faced the same problem.

I performed the following steps to solve this problem :

  1. Locate the js file into "compile source" section in the build settings and remove it from there.

  2. Locate "copy Bundle Resource" section and add the js file there.

The above 2 steps solved my problem..

我也只是我 2024-10-16 12:25:10

有时 xCode 没有意识到您的项目中存在文件,并且没有正确地将所有内容捆绑在一起。尝试删除 .js 文件并再次添加。

另外,您似乎提到了两个不同的文件名:activity.js 和 global.js

sometimes xCode doesn't realize a file is in your project, and doesn't bundle everything up correctly. try removing the .js file and adding it again.

also, it seems that you are mentioning 2 different file names, activity.js and global.js

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