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."
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(){ $0.lastPathComponent! }.filter(){ $0.pathExtension == "mp3" } // error is here
}
}
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
}
}
Xcode 8.3.2 • Swift 3.1
extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
var pathExtension: String {
return fileURL.pathExtension
}
var lastPathComponent: String {
return fileURL.lastPathComponent
}
}
Base on this link , this extension will help you in many case.
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)
}
}
Just change the order: first filter
, than map
. Now the method pathExtension
is applied to the NSURL objects.
listOfMP3Files = directoryURLs.filter { $0.pathExtension == "mp3" }.map { $0.lastPathComponent! }
You can omit the pair of parentheses after filter
and map
using the trailing closure
rule.
Apple has removed the path related API from String
to prefer the more suitable URL
.
I combined Dharmesh and Leo's approaches to get my extension to work:
extension String {
public var url: NSURL {
return NSURL(fileURLWithPath:self)
}
public var stringByDeletingLastPathComponent: String {
return String(url.URLByDeletingLastPathComponent)
}
}
Swift Doc 1.2 specs regarding these funcs don't return an optional
To make it backward compatible to 1.2 in Swift 2.0, do this:
Improve upon @Dharmesh Kheni 's version.
extension String {
var ns: NSString {
return self as NSString
}
var pathExtension: String {
return ns.pathExtension ?? ""
}
var lastPathComponent: String {
return ns.lastPathComponent ?? ""
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.