【问题标题】:How to do Key-Value Observing for NSProgress in Swift如何在 Swift 中对 NSProgress 进行键值观察
【发布时间】:2016-05-03 07:20:21
【问题描述】:

我目前正在使用owncloud iOS SDK 将文件上传到我的私有云。我正在尝试将 Key-Value 观察机制 provided in this example 引入 Swift。

库强制我在上传方法中传递一个指向NSProgress 对象的指针。然后我想做一个 Key Value Observing 并相应地更新我的 UI。

class OwnCloudManager : NSObject {

    //Key Value observation requires the dynamic keyword
    //NSProgress must also extend from NSObject
    dynamic var progress: NSProgress = NSProgress()
    let apiURL : NSString = "https://cloud.example.com/remote.php/webdav/"
    let ocCommunication = OCCommunication()

    override init() {
      //configure ocCommunication here, set user name, password
      //and adjust ocCommunication.securityPolicy
      //...
    }
    deinit {
        //remove observer when object is destroyed
        progress.removeObserver(self, forKeyPath: "fractionCompleted")
    }

    func uploadFile(folderURL : NSURL, filename: String) -> NSURLSessionUploadTask?{

        let fileURL = apiURL.stringByAppendingPathComponent(filename)
        let progressPointer = AutoreleasingUnsafeMutablePointer<NSProgress?>.init(&progress)


        let uploadTask = ocCommunication.uploadFileSession(folderURL.path, toDestiny: fileURL, onCommunication: ocCommunication,
            withProgress: progressPointer,
            successRequest: ({(response, redirectedServer) in
                //...

            }),
            failureRequest: ({ (response, redirectedServer, error) in
                //request failed while execution
            }),
            failureBeforeRequest: ({ error in
                //failure before request is executed

            })
        )

        progress.addObserver(self, forKeyPath: "fractionCompleted", options: .New, context: nil)

        uploadTask.resume()
        return uploadTask
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        if let observedProgress = object as? NSProgress where keyPath == "fractionCompleted" {
            //call my delegate here that updates the UI
        }
        else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }  
    }
}

我在观察方法中设置了一个断点,但不幸的是它从未被调用过。谁能告诉我如何解决这个问题?

【问题讨论】:

    标签: ios swift key-value-observing owncloud


    【解决方案1】:

    查看此代码以了解 Progressing..!!

    override func  observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
            if keyPath == "fractionCompleted"{
                let progress : NSProgress = object as! NSProgress
                print(progress)
    
            }
    
        }
    

    【讨论】:

    • 刚刚尝试过,但从未调用过该方法。整体上传成功,调用成功回调但我无法跟踪进度。
    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多