Coder Social home page Coder Social logo

Comments (5)

srdanrasic avatar srdanrasic commented on August 23, 2024

If will fire if you change its dynFrame property.

If you really need to observe frame changes, you could create a Dynamic from KVO:

class customView:UIView{
   ...

   lazy var frameListener:Bond<CGRect> = Bond<CGRect>(){ [unowned self] frame in
        println("firing :D")
    }

    func bind() {
        dynamicObservableFor(self, keyPath: "frame", defaultValue: CGRectZero) ->| frameListener
    }
}

https://github.com/SwiftBond/Bond#key-value-observing

from bond.

whateverx avatar whateverx commented on August 23, 2024

Thank you, got it for dynFrame

Using Bond 3.7.2, I was playing with dynamicObservableFor but it throw me an error during the cast of the generic T on this line

public func dynamicObservableFor<T>(object: NSObject, #keyPath: String, #defaultValue: T) -> Dynamic<T> {
...
let value: T = (keyPathValue != nil) ? (keyPathValue as? T)! : defaultValue

Could not cast value of type 'NSConcreteValue' (0x1740576b8) to 'C.CGRect' (0x1002c5f80)

i'll try to find out what is going wrong

from bond.

whateverx avatar whateverx commented on August 23, 2024

I had to use NSValue for the defaultValue and map to CGRect because object.valueForKeyPath(keyPath)return an NSValue
now it works great !

dynamicObservableFor(self, keyPath: "frame", defaultValue:NSValue(CGRect: CGRectZero)).map{$0.CGRectValue()} ->| frameListener

is there a better way ?

from bond.

srdanrasic avatar srdanrasic commented on August 23, 2024

Yeah, that's due to unfortunate differences between ObjC and Swift. There is another way to create Dynamic from KVO property. It allows you to manually convert object. You need to pass two closures. One that converts AnyObject? to CGRect, and one other way round.

dynamicObservableFor(self, keyPath: "frame", from: { $0?.CGRectValue() ?? CGRectZero }, to: { NSValue(CGRect: $0) })

from bond.

whateverx avatar whateverx commented on August 23, 2024

The second way seems to be better
You've done an amazing job with Swift Bond, thank you!

from bond.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.