xcode Swift 3 中 NSURL.URLByAppendingPathComponent() 的等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38812240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the Swift 3 equivalent of NSURL.URLByAppendingPathComponent()?
提问by Pekka
I'm following a basic tutorialon building a simple iOS app in Swift.
我正在学习在 Swift 中构建简单 iOS 应用程序的基本教程。
It is written in Swift 2.x, and I work with XCode 8 Beta and Swift 3.
它是用 Swift 2.x 编写的,我使用 XCode 8 Beta 和 Swift 3。
The tutorial suggests using NSFileManager
to find a data directory. Class names have changed, so the auto-fixed Swift 3 looks like this:
本教程建议使用NSFileManager
来查找数据目录。类名已更改,因此自动修复的 Swift 3 如下所示:
static let DocumentsDirectory = FileManager().urlsForDirectory(.documentDirectory, inDomains:.userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals")
However, Xcode now complains that
然而,Xcode 现在抱怨说
Value of type 'URL' has no member 'URLByAddingPathComponent'
I am unable to find out what the method is called now.
我现在无法找出该方法的名称。
The NSURL Class Referencedoesn't contain any hints as to how to address it from Swift 3
该NSURL类引用不包含任何提示如何从斯威夫特3解决这一问题
What is the new method name?
Where do I have to go to find a complete class reference for Swift 3 (or, the Swift 3 interface to the library the
URL
class is defined in - I still don't completely understand the nomenclature) so I can research these myself in the future?
新方法名称是什么?
我必须去哪里找到 Swift 3 的完整类参考(或者,Swift 3
URL
类定义的库的接口- 我仍然不完全理解命名法)以便我将来可以自己研究这些?
回答by Hamish
As of Xcode 8 beta 4, it is named appendingPathComponent(_:)
, and does not throw.
从 Xcode 8 beta 4 开始,它被命名为appendingPathComponent(_:)
,并且不会抛出。
static let archiveURL = documentsDirectory.appendingPathComponent("meals")
Also as Leo Dabus points out in the comments, your documentsDirectory
property will need changing to use urls(for:in:)
in beta 4:
此外,正如Leo Dabus 在评论中指出的那样,您的documentsDirectory
财产需要更改以urls(for:in:)
在 beta 4 中使用:
static let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
(Note that I've made your property names lowerCamelCase
, as per the Swift API design guidelines. I would also recommend using FileManager.default
, rather than creating a new instance.)
(请注意,我已经lowerCamelCase
根据Swift API 设计指南FileManager.default
创建了您的属性名称。我还建议使用,而不是创建新实例。)
You can take a look at Apple's latest API reference guideto see the API naming changes that have taken place in Swift 3.
您可以查看Apple 的最新 API 参考指南,了解 Swift 3 中发生的 API 命名更改。
回答by Khundragpan
It has now changed to appendingPathComponent(_:)
and it throws, so you need to wrap it in do - catch block
它现在已更改为appendingPathComponent(_:)
并抛出,因此您需要将其包装在 do - catch 块中
do {
let archiveURL = try documentsDirectory?.appendingPathComponent("meals")
} catch {
print(error)
}
Update
更新
As per Xcode 8 beta 4, the appendingpathcomponent(_:)
do not throw error.
根据 Xcode 8 beta 4,appendingpathcomponent(_:)
不要抛出错误。
For relevant info see answer by @Hamish
有关相关信息,请参阅@Hamish 的回答
回答by Girish Chovatiya
func appendingPathComponent(String)
=> Returns a new URL made by appending a path component to the original URL.
=> 返回通过将路径组件附加到原始 URL 所形成的新 URL。
static let archiveURL = documentsDirectory?.appendingPathComponent("meals")
If it is directory:
如果是目录:
func appendingPathComponent(String, isDirectory: Bool)
=> Returns a new URL made by appending a path component to the original URL, along with a trailing slash if the component is designated a directory.
=> 返回通过将路径组件附加到原始 URL 以及尾部斜杠(如果组件被指定为目录)构成的新 URL。
static let archiveURL = documentsDirectory?.appendingPathComponent("meals", isDirectory: true)