如何在Swift中将此var字符串转换为URL

编程入门 行业动态 更新时间:2024-10-26 04:32:21
本文介绍了如何在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

本文发布于:2023-11-01 12:11:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将此   字符串   转换为   如何在   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!