ios - Bundle.main.path(forResource :ofType:inDirectory:) returns nil when app name contains "space"

标签 ios swift3

为什么 Bundle.main.path(forResource: "testFile", ofType: "rtf")当应用程序名称有空格时返回 nil?有人遇到过这个吗?也许能够修复?如果我删除应用程序名称中的“空格”,则不会发生错误。

当我打印路径时,结果是 /Users/<user>/Library/Developer/CoreSimulator/Devices/915D4A93-D812-4180-A49E-6BFA3BD77986/data/Containers/Bundle/Application/8623072B-7528-463C-971F-ECD1FB89BDDB/Test Application.app/testFile.rtf您可能会注意到该应用程序的名称“Test Application.app”上有空格。这会导致 Bundle.main.path返回零

最佳答案

无论如何都强烈建议使用 URL 相关的 API

Bundle.main.url(forResource: "testFile", withExtension: "rtf") 

关于ios - Bundle.main.path(forResource :ofType:inDirectory:) returns nil when app name contains "space",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46293390/

相关文章:

ios - 如何从单独的 View Controller 调用 popToRootViewController() 函数?

ios - 无法识别 POST 正文值

swift - 将字符串转换为 NSObject Swift 3

arrays - 在 swift 3 中使用 Json

ios - Swift 3 中的闭包速记语法

addPersistentStoreWithType 的 iOS 核心数据 + iCloud 错误 : You don’t have permission to save the file “store” in the folder

ios - 将 UIView 部分滑动以暴露另一个 View 并使暴露的 View 可交互?

ios - 带导航栏的 MKMapView 内容插入

iphone - 按下主页按钮时如何显示红色状态栏?

swift - 将图像上传到 Firebase 存储和数据库