开发者

Most efficient way to convert the x value of an NSValue CGPointValue into an NSNumber

开发者 https://www.devze.com 2023-04-05 06:23 出处:网络
I have an array with a bunch of NSV开发者_如何学运维alue objects. These objects are essentially wrappers for CGPoints. I need to create an NSDictionary out of these CGPoints that records those points

I have an array with a bunch of NSV开发者_如何学运维alue objects. These objects are essentially wrappers for CGPoints. I need to create an NSDictionary out of these CGPoints that records those points which have the greatest x value for a given y value. For example, there may be multiple points that share the same y value, but only that which has the greatest x value for that y value should be recorded in the NSDictionary. Ultimately, the y value is the key and the x value is the value of that key.

As I loop through the array of NSValue objects, I need to compare their x values to the x value of an NSDictionary entry that has the same y value (and if that does not exist, create the entry). In order to do this, I use the objectForKey method of the NSDictionary object. But this method requires an object of type id (a nondescript object). Which brings me to the issue.

That object of type id that I'm going to pass to the objectForKey method is simply the y value of a CGPoint. But I get that y value by first converting an NSValue object to a CGPoint. So once I've done that, I have to convert that y value back into an object (i.e. NSNumber numberWithFloat). This seems kind of inefficient to me -- converting an object to a CGPoint in order to extract a CGFloat that then gets turned back into an object via NSNumber.

Is there a better way to do this?

Here's my current code:

for (NSValue *pt in myArrayOfPoints) {
    NSNumber *y = [NSNumber numberWithFloat:[pt CGPointValue].y];
    if ([leftSideEdgePtsFurthestInwards objectForKey:y] == nil) {
    //yada yada yada
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号