Swift 3中等效的NSURL.URLByAppendingPathComponent()是什么? [英] What is the Swift 3 equivalent of NSURL.URLByAppendingPathComponent()?

查看:474
本文介绍了Swift 3中等效的NSURL.URLByAppendingPathComponent()是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注有关在Swift中构建简单iOS应用的基本教程.

它是用Swift 2.x编写的,并且我使用XCode 8 Beta和Swift3.

本教程建议使用NSFileManager查找数据目录.类名已更改,因此自动修复的Swift 3如下所示:

static let DocumentsDirectory = FileManager().urlsForDirectory(.documentDirectory, inDomains:.userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals")

但是,Xcode现在抱怨

Value of type 'URL' has no member 'URLByAddingPathComponent'

我现在无法找出该方法的名称.

NSURL类参考没有没有任何有关如何从Swift 3解决该问题的提示

  • 新方法名称是什么?

  • 我必须在哪里找到Swift 3的完整类引用(或者,URL类的库的Swift 3接口在其中定义-我仍然不完全了解该术语)这样我以后就可以自己研究这些了?

解决方案

从Xcode 8 beta 4开始,它被命名为

Leo Dabus指出注释,则您的documentsDirectory属性需要更改为使用 在Beta 4中:

static let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

(请注意,根据 Apple最新的API参考指南,以查看已进行的API命名更改放置在Swift 3中.

I'm following a basic tutorial on building a simple iOS app in Swift.

It is written in Swift 2.x, and I work with XCode 8 Beta and 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:

static let DocumentsDirectory = FileManager().urlsForDirectory(.documentDirectory, inDomains:.userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals")

However, Xcode now complains that

Value of type 'URL' has no member 'URLByAddingPathComponent'

I am unable to find out what the method is called now.

The NSURL Class Reference doesn't contain any hints as to how to address it from Swift 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?

解决方案

As of Xcode 8 beta 4, it is named appendingPathComponent(_:), and does not throw.

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:

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.)

You can take a look at Apple's latest API reference guide to see the API naming changes that have taken place in Swift 3.

这篇关于Swift 3中等效的NSURL.URLByAppendingPathComponent()是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆