Swift 2.0:NSBundle 总是 returns 无
Swift 2.0 : NSBundle always returns nil
我正在实施这个 git - NSDate-TimeAgo
里面有 swift 个扩展名。我已将 git 提供的 Bundle 拖放到我的应用程序中 - NSDateTimeAgo.bundle
在扩展文件中,我试图获取此文件路径,但它总是 return nil
SWIFT2.0
func NSDateTimeAgoLocalizedStrings(key: String) -> String {
let resourcePath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = resourcePath.URLByAppendingPathComponent("NSDateTimeAgo.bundle")
let bundle = NSBundle(URL: path)
print(bundle) -> **nil**
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle!, comment: "")
}
有什么建议吗?
必须将捆绑包复制到您的 resources 文件夹中,而不是复制到应用捆绑包的顶层。
这一行错了:
let resourcePath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
捆绑包不在您的文档目录中。它在您的应用程序中。看看实际代码做了什么(在NSDate+Extension.swift):
func NSDateTimeAgoLocalizedStrings(key: String) -> String {
// LOOK!!!!
let resourcePath = NSBundle.mainBundle().resourcePath
let path = resourcePath?.stringByAppendingPathComponent("NSDateTimeAgo.bundle")
let bundle = NSBundle(path: path!)
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle!, comment: "")
}
基本上你应该让它这样做。不要自己弄乱捆绑包。只需安装 NSDate+Extension.swift 和捆绑包,然后停止。不要更改代码 - 你所做的只是破坏它。
如果接受的方法无效 - 找到解决方案 -
guard let resourcePath = NSBundle.mainBundle().resourcePath else {
return ""
}
let path = NSURL(fileURLWithPath:resourcePath).URLByAppendingPathComponent("NSDateTimeAgo.bundle")
guard let bundle = NSBundle(URL: path) else {
return ""
}
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle, comment: "")
我正在实施这个 git - NSDate-TimeAgo
里面有 swift 个扩展名。我已将 git 提供的 Bundle 拖放到我的应用程序中 - NSDateTimeAgo.bundle
在扩展文件中,我试图获取此文件路径,但它总是 return nil SWIFT2.0
func NSDateTimeAgoLocalizedStrings(key: String) -> String {
let resourcePath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = resourcePath.URLByAppendingPathComponent("NSDateTimeAgo.bundle")
let bundle = NSBundle(URL: path)
print(bundle) -> **nil**
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle!, comment: "")
}
有什么建议吗?
必须将捆绑包复制到您的 resources 文件夹中,而不是复制到应用捆绑包的顶层。
这一行错了:
let resourcePath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
捆绑包不在您的文档目录中。它在您的应用程序中。看看实际代码做了什么(在NSDate+Extension.swift):
func NSDateTimeAgoLocalizedStrings(key: String) -> String {
// LOOK!!!!
let resourcePath = NSBundle.mainBundle().resourcePath
let path = resourcePath?.stringByAppendingPathComponent("NSDateTimeAgo.bundle")
let bundle = NSBundle(path: path!)
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle!, comment: "")
}
基本上你应该让它这样做。不要自己弄乱捆绑包。只需安装 NSDate+Extension.swift 和捆绑包,然后停止。不要更改代码 - 你所做的只是破坏它。
如果接受的方法无效 - 找到解决方案 -
guard let resourcePath = NSBundle.mainBundle().resourcePath else {
return ""
}
let path = NSURL(fileURLWithPath:resourcePath).URLByAppendingPathComponent("NSDateTimeAgo.bundle")
guard let bundle = NSBundle(URL: path) else {
return ""
}
return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle, comment: "")