【问题标题】:Implementing PHPhotoLibraryChangeObserver protocol in swift快速实现 PHPhotoLibraryChangeObserver 协议
【发布时间】:2016-03-03 16:49:15
【问题描述】:

我正在尝试将我的 AssetService 设置为 changeObserver,但出现以下错误:

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

虽然 photoLibraryDidChange 是唯一需要的方法。这是我的代码:

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(), {

        })
    }
}

【问题讨论】:

    标签: swift photosframework swift-protocols


    【解决方案1】:

    我认为您需要从 NSObject 扩展才能在 PhotoFramework 中使用它

    因此你还需要重写 init 并添加 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(), {
    
            })
        }
    }
    

    希望能解决这个问题

    【讨论】:

      【解决方案2】:

      在 Swift 3.0 中,寄存器现在实际上是这样的:

      func photoLibraryDidChange(_ changeInstance: PHChange) {
          DispatchQueue.main.async {
      
          }
      }
      
      public override init() {
          super.init()
          PHPhotoLibrary.shared().register(self)
      }
      

      Bart Schoon's answer 中的其他所有内容都相同

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多