How to cast within #keyPath() ?

How can I cast a path segment within #keyPath() ?

For example in the following code:

···

——

import Cocoa

class MyView: NSView {
    var property: String?
}

class MyViewController: NSViewController {
    
    var viewProperty: String? {
        return (view as! MyView).property // 1)
    }
    
    override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
        var keyPaths = super.keyPathsForValuesAffectingValue(forKey: key)
    
        switch key {
        case #keyPath(viewProperty):
            keyPaths.formUnion(#keyPath(view.property)) // 2)
        default:
            break
        }
    
        return keyPaths
    }
}

——

In the line marked 1) I can cast view to MyView to access a property. In the line marked 2) I don't know how to perform that cast, and without I get the error "Type 'NSView' has no member 'property'".

How can I solve this problem?

Thanks,

Manfred

#keyPath(MyView.property) should do it, I’d think.

Charles

···

On Jul 25, 2017, at 11:16 AM, Manfred Schubert via swift-users <swift-users@swift.org> wrote:

How can I cast a path segment within #keyPath() ?

For example in the following code:

——

import Cocoa

class MyView: NSView {
   var property: String?
}

class MyViewController: NSViewController {

   var viewProperty: String? {
       return (view as! MyView).property // 1)
   }

   override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
       var keyPaths = super.keyPathsForValuesAffectingValue(forKey: key)

       switch key {
       case #keyPath(viewProperty):
           keyPaths.formUnion(#keyPath(view.property)) // 2)
       default:
           break
       }

       return keyPaths
   }
}

——

In the line marked 1) I can cast view to MyView to access a property. In the line marked 2) I don't know how to perform that cast, and without I get the error "Type 'NSView' has no member 'property'".

How can I solve this problem?

Thanks,

Manfred
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I think it wouldn't. It returns "property", but the path needs to be "view.property".

Kind regards,

Manfred

···

Am 25.07.2017 um 18:21 schrieb Charles Srstka <cocoadev@charlessoft.com>:

#keyPath(MyView.property) should do it, I’d think.

Ah, you’re right. This should work, though:

“\(#keyPath(view)).\(#keyPath(MyView.property))”

Charles

···

On Jul 25, 2017, at 11:26 AM, Manfred Schubert <dev@schubert-it.com> wrote:

Am 25.07.2017 um 18:21 schrieb Charles Srstka <cocoadev@charlessoft.com>:

#keyPath(MyView.property) should do it, I’d think.

I think it wouldn't. It returns "property", but the path needs to be "view.property”.

Yes, that should work. Thank you, Charles!

Manfred

···

Am 25.07.2017 um 18:31 schrieb Charles Srstka <cocoadev@charlessoft.com>:

On Jul 25, 2017, at 11:26 AM, Manfred Schubert <dev@schubert-it.com> wrote:

Am 25.07.2017 um 18:21 schrieb Charles Srstka <cocoadev@charlessoft.com>:

#keyPath(MyView.property) should do it, I’d think.

I think it wouldn't. It returns "property", but the path needs to be "view.property”.

Ah, you’re right. This should work, though:

“\(#keyPath(view)).\(#keyPath(MyView.property))”

Terms of Service

Privacy Policy

Cookie Policy