Swift Extensions

From PeformIQ Upgrade
Revision as of 21:56, 16 October 2021 by PeterHarding (talk | contribs) (Created page with "These often provide a succinct way of cleanly extending existing classes. =PHAsset= From - http://stackoverflow.com/questions/27854937/ios8-photos-framework-how-to-get-the-...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

These often provide a succinct way of cleanly extending existing classes.


PHAsset

From - http://stackoverflow.com/questions/27854937/ios8-photos-framework-how-to-get-the-nameor-filename-of-a-phasset



extension PHAsset {
    
    var originalFilename: String? {
        
        var fname: String?
        
        if #available(iOS 9.0, *) {
            let resources = PHAssetResource.assetResources(for: self)
            if let resource = resources.first {
                fname = resource.originalFilename
            }
        }
        
        if fname == nil {
            // This is an undocumented workaround that works as of iOS 9.1
            fname = self.value(forKey: "filename") as? String
        }
        
        return fname
    }
}