Swift Extensions

From PeformIQ Upgrade
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
    }
}