Marijn
Marijn

Reputation: 1479

Implementing PHPhotoLibraryChangeObserver protocol in swift

I'm trying to set my AssetService as changeObserver, but I get the folowing error:

Error:(8, 14) type 'AssetService' does not conform to protocol 'PHPhotoLibraryChangeObserver'

While photoLibraryDidChange is the only required method. Here's my code:

import UIKit
import Photos

public class AssetService : PHPhotoLibraryChangeObserver {

    public init() {

        // here I do some other stuff
        PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
    }

    public func photoLibraryDidChange(changeInstance: PHChange) {
        dispatch_async(dispatch_get_main_queue(), {

        })
    }
}

Upvotes: 4

Views: 3723

Answers (2)

John Riselvato
John Riselvato

Reputation: 12924

In Swift 3.0 the register actually looks like this now:

func photoLibraryDidChange(_ changeInstance: PHChange) {
    DispatchQueue.main.async {

    }
}

public override init() {
    super.init()
    PHPhotoLibrary.shared().register(self)
}

Everything else is the same in Bart Schoon's answer

Upvotes: 3

Bart Schoon
Bart Schoon

Reputation: 309

I think you need to extend from NSObject in order to use it in the PhotoFramework

Therefore you need also to override the init and add super.init()

import UIKit 
import Photos

public class AssetService : NSObject, PHPhotoLibraryChangeObserver {
     public override init() {
         super.init()
         // here I do some other stuff
         PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
     }

    public func photoLibraryDidChange(changeInstance: PHChange) {
        dispatch_async(dispatch_get_main_queue(), {

        })
    }
}

Hope this will solve it

Upvotes: 5

Related Questions