How to split filename from file extension in Swift?

前端 未结 21 1911
陌清茗
陌清茗 2020-12-01 03:24

Given the name of a file in the bundle, I want load the file into my Swift app. So I need to use this method:

let soundURL = NSBundle.mainBundle().URLForReso         


        
21条回答
  •  遥遥无期
    2020-12-01 04:09

    A cleaned up answer for Swift 4 with an extension off of PHAsset:

    import Photos
    
    extension PHAsset {
        var originalFilename: String? {
            if #available(iOS 9.0, *),
                let resource = PHAssetResource.assetResources(for: self).first {
                return resource.originalFilename
            }
    
            return value(forKey: "filename") as? String
        }
    }
    

    As noted in XCode, the originalFilename is the name of the asset at the time it was created or imported.

提交回复
热议问题