swift - 什么会导致 Xcode MacOs 应用程序将主包指向 "/Applications/Xcode.app"?

标签 swift xcode macos

我正在努力编写我的第一个 Swift3 应用程序 - 一个屏幕保护程序,因为我无法引用任何本地文件。经过几个小时的调试和设置 Playground,我发现 Bundle.main.bundlePath 没有指向我的应用程序,而是指向“/Applications/Xcode.app”。这解释了为什么在下面的代码中 - video.mp4 从未找到,但我也从按标识符选择 Bundle 中得到了 nil。

关于如何解决这个问题有什么建议吗?我的一个本地文件 (video.mp4) 是可读的,复制为 Bundle Resource 并且没有伪造的 xattrs。我卡住了!任何帮助表示赞赏。

运行 10.12.3/Xcode 8.2.1,Swift 3.0.2

Playground 代码如下:

import Cocoa
import PlaygroundSupport

import AVKit
import AVFoundation
var frameRect = NSRect(x: 0, y: 0, width: 600, height: 600)
var mainView = NSView(frame: frameRect)


let bundle = Bundle(identifier: "com.modprods.koorigras-test.Playground")
debugPrint(bundle)

debugPrint(Bundle.main.bundlePath)
guard let path = Bundle.main.path(forResource: "video",ofType:"mp4") else {
    debugPrint("not found")
    exit(1)
}

最佳答案

根据 Eric 的评论,这是 Playground 的正确行为。

对于其他 n00bs,在您的工作区内创建一个新的 Playground 会导致 Sources 和 Resources 文件夹变灰,如果您尝试在 finder 中显示它们,则什么也不会发生(因为它们不存在)。从新的 Playground 中访问本地文件

  • 创建 Resources 子文件夹
  • 将 Assets 复制到资源
  • 确保 Copy Bundle Resources 包含目标 Assets

似乎无法从 Playground 中访问其他包。

关于swift - 什么会导致 Xcode MacOs 应用程序将主包指向 "/Applications/Xcode.app"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975717/

相关文章:

ios - Xcode 6.1 - 无法打开 Nib,因为您无权查看它

xcode - Xcode 9.1启动失败-UNCAUGHT EXCEPTION(NSInternalInconsistencyException)

ios - 使用带有 Xcode 8.2 的 swift 3 扩展和折叠 tableview 单元格的问题

cocoa - 我们应该在 Mac App Store 应用程序中设计的标准屏幕分辨率是多少?

swift - 如何在 init "content"属性中计算和初始化? swift

ios - 在 Swift 中的 WebService 中传递参数

swift - Firebase连续上传中途停止,只上传最后一张图片

ios - 设置是否没有观察模型属性的变化 - Swift

objective-c - 如何改变 NSTableView 滚动条的外观

python - 阅读我自己的印象笔记