如何在Swift中将此var字符串转换为URL [英] How to convert this var string to URL in Swift
本文介绍了如何在Swift中将此var字符串转换为URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要url文件路径为 URL (在旧版Swift中为NSURL).我有这个:
I need url filepath be a URL (NSURL in old versions of Swift). I have this:
let paths = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true)
// NSString *documentsDirectory = [paths objectAtIndex:0];
let documentsDirectory = paths[0] as String
var filePath:String? = nil
var fileNamePostfix = 0
repeat {
filePath =
"\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix).mp4"
fileNamePostfix += 1
} while (FileManager.default.fileExists(atPath: filePath))
我需要将其转换为URL,以便在self.fileOutput.startRecording(to: <#outputFileURL: URL?#>, recordingDelegate: <#AVCaptureFileOutputRecordingDelegate?#>)
方法中使用.
I need to convert this to URL for use in self.fileOutput.startRecording(to: <#outputFileURL: URL?#>, recordingDelegate: <#AVCaptureFileOutputRecordingDelegate?#>)
method.
我尝试了filePath as? URL()
,但这是不正确的.
I tried filePath as? URL()
but it isn't correct.
推荐答案
您需要这样做:
let fileUrl = URL(string: filePath)
或
let fileUrl = URL(fileURLWithPath: filePath)
取决于您的需求.请参见 URL文档
depending on your needs. See URL docs
在Swift 3之前,URL被称为NSURL.
Before Swift 3, URL was called NSURL.
这篇关于如何在Swift中将此var字符串转换为URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文