xcode pathExtension 不可用:改为在 NSURL 上使用 pathExtension。斯威夫特 2.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32646168/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:50:42  来源:igfitidea点击:

pathExtension is unavailable: Use pathExtension on NSURL instead. Swift 2.0

iosxcodeswiftswift2

提问by Alexander Khitev

I get mp3 files from Document Directory, I wrote that pathExtension equals "mp3", in the Swift 1.2 it is working, but in the Swift 2.0 I get the error " 'pathExtension' is unavailable: Use pathExtension on NSURL instead."

我从文档目录中获取 mp3 文件,我写道 pathExtension 等于“mp3”,在 Swift 1.2 中它正在工作,但在 Swift 2.0 中我收到错误“'pathExtension' 不可用:改为在 NSURL 上使用 pathExtension。”

 func fetchFilesFromFolder() {
        let fileManager = NSFileManager.defaultManager()
        let folderPathURL = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)[0]
        if let directoryURLs = try? fileManager.contentsOfDirectoryAtURL(folderPathURL, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles) {
            listOfMP3Files = directoryURLs.map(){ 
listOfMP3Files = directoryURLs.filter { 
extension String {
    var ns: NSString {
        return self as NSString
    }
    var pathExtension: String {
        return ns.pathExtension
    }
    var lastPathComponent: String {
        return ns.lastPathComponent
    }
}
.pathExtension == "mp3" }.map {
extension String {
    var fileURL: URL {
        return URL(fileURLWithPath: self)
    }
    var pathExtension: String {
        return fileURL.pathExtension
    }
    var lastPathComponent: String {
        return fileURL.lastPathComponent
    }
}
.lastPathComponent! }
.lastPathComponent! }.filter(){
extension String {

    var lastPathComponent: String {
        get {
            return (self as NSString).lastPathComponent
        }
    }

    var pathExtension: String {
        get {

            return (self as NSString).pathExtension
        }
    }

    var stringByDeletingLastPathComponent: String {
        get {

            return (self as NSString).stringByDeletingLastPathComponent
        }
    }

    var stringByDeletingPathExtension: String {
        get {

            return (self as NSString).stringByDeletingPathExtension
        }
    }

    var pathComponents: [String] {
        get {

            return (self as NSString).pathComponents
        }
    }

    func stringByAppendingPathComponent(path: String) -> String {

        let nsSt = self as NSString

        return nsSt.stringByAppendingPathComponent(path)
    }

    func stringByAppendingPathExtension(ext: String) -> String? {

        let nsSt = self as NSString

        return nsSt.stringByAppendingPathExtension(ext)
    }
}
.pathExtension == "mp3" } // error is here } }

采纳答案by vadian

Just change the order: first filter, than map. Now the method pathExtensionis applied to the NSURL objects.

只需更改顺序: first filter, than map。现在该方法pathExtension应用于 NSURL 对象。

  extension String { 
      public var url: NSURL {
         return NSURL(fileURLWithPath:self)
      }
      public var stringByDeletingLastPathComponent: String {
         return String(url.URLByDeletingLastPathComponent)
      }
 }

You can omit the pair of parentheses after filterand mapusing the trailing closurerule.

您可以后省略对括号filtermap使用trailing closure规则。

Apple has removed the path related API from Stringto prefer the more suitable URL.

Apple 已经删除了与路径相关的 APIString以选择更合适的URL.

回答by Dharmesh Kheni

Or you can use this extension:

或者你可以使用这个扩展:

extension String {
  var ns: NSString {
    return self as NSString
  }
  var pathExtension: String {
    return ns.pathExtension ?? ""
 }
  var lastPathComponent: String {
    return ns.lastPathComponent ?? ""
   }
}

回答by Leo Dabus

Xcode 8.3.2 ? Swift 3.1

Xcode 8.3.2 ? 斯威夫特 3.1

##代码##

回答by hqt

Base on this link, this extension will help you in many case.

基于此链接,此扩展程序将在许多情况下为您提供帮助。

##代码##

回答by Matt Bearson

I combined Dharmesh and Leo's approaches to get my extension to work:

我结合了 Dharmesh 和 Leo 的方法来让我的扩展工作:

##代码##

回答by Kevin

Swift Doc 1.2 specs regarding these funcs don't return an optional

关于这些函数的 Swift Doc 1.2 规范不返回可选

To make it backward compatible to 1.2 in Swift 2.0, do this:

要使其向后兼容 Swift 2.0 中的 1.2,请执行以下操作:

Improve upon @Dharmesh Kheni 's version.

改进@Dharmesh Kheni 的版本。

##代码##