Accessing ALAssetsLibrary in Swift
I recently ran into an issue for an app I’m building when trying to access the AssetsLibrary using Swift.  The error I kept getting was :
invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary
For obj-c developers you just need to create a Singleton using dispatch_once() to create persistent access to the library. Â
The gist of the code looks like this:
+ (ALAssetsLibrary *)defaultAssetsLibrary { Â Â static dispatch_once_t pred = 0; Â Â static ALAssetsLibrary *library = nil; Â Â dispatch_once(&pred, ^{ Â Â Â Â library = [[ALAssetsLibrary alloc] init]; Â Â }); Â Â return library; }
In Swift I rewrote it like this:
class AssetsLib : ALAssetsLibrary { Â Â class var sharedInstance : AssetsLib{ Â Â Â Â struct Static{ Â Â Â Â Â Â static let instance: AssetsLib = AssetsLib() Â Â Â Â }
    return Static.instance   }
  override init(){
  } }
and here’s how i called it:
let library = AssetsLib.sharedInstance
Hope this helps!







