错误:-observeValueForKeyPath:ofObject:change:context:收到消息但未处理

时间:2017-11-07 05:28:21

标签: ios swift xcode

我想根据内容设置集合视图的动态高度,我尝试下面的代码:

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    self.ItemCollection.addObserver(self , forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)


}


func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutableRawPointer) {

    let newHeight : CGFloat = self.ItemCollection.collectionViewLayout.collectionViewContentSize.height

    var frame : CGRect! = self.ItemCollection.frame
    frame.size.height = newHeight

    self.ItemCollection.frame = frame


}    

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

            self.ItemCollection.removeObserver(self, forKeyPath: "contentSize")
}

我在这一行收到错误:

 self.ItemCollection.addObserver(self , forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)

以下是错误:

An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: contentSize
Observed object: <UICollectionView: 0x102911200; frame = (0 275; 375 360);     clipsToBounds = YES; autoresize = W+TM+H+BM; gestureRecognizers = <NSArray: 0x1c4247a40>; layer = <CALayer: 0x1c02388a0>; contentOffset: {0, 0}; contentSize: {375, 0}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x102543a80>
Change: {
kind = 1;
old = "NSSize: {20, 360}";
}
Context: 0x0'

1 个答案:

答案 0 :(得分:4)

Swift 4 中,它的覆盖功能应该是 -

Dim symbols = "!""$%^&()_-"

Dim plusses = { _
        password.Any(Function(c) symbols.Contains(c)), _
        password.Any(Function(c) Char.IsDigit(c)), _
        password.Any(Function(c) Char.IsUpper(c)), _
        password.Any(Function(c) Char.IsLower(c)) _
    }.Where(Function (x) x).Count() * 5

Dim minusses = { _
        password.All(Function(c) symbols.Contains(c)), _
        password.All(Function(c) Char.IsDigit(c)), _
        password.All(Function(c) Char.IsUpper(c)), _
        password.All(Function(c) Char.IsLower(c)) _
    }.Where(Function (x) x).Count() * 5     

Dim score = plusses - minusses