Search code examples
iosreactive-cocoa

Q:How to get CGPoint from NSConcreteValue?


I use RAC in my project to observe tableView's contentOffset, but I don't know how to extract the x, because the id's type is NSConcreteValue:

[RACObserve(_tableview, contentOffset)
        subscribeNext:^(id  x){

            float currentOffsetY = x.y; // this line is wrong code.

     }];

I have referenced related questions: Get value from KVO - returning NSConreteValue


Solution

  • What you are looking for is the pointValue property on NSValue. So you just need to cast the x parameter to NSValue because NSConcreteValue is a subclass of NSValue. Then you can extract the desired value from the CGPoint struct.

    Background information

    If you want to treat a C struct (e.g. CGPoint or CGRect) as an Objective-C Object, e.g., to add it to an NSArray, you need to wrap it in an object. Therefore, Apple has provided special initializers and getter on NSValue.