Question

I have a NSMutableArray in which I would like to store some CGRects:

So, I tried using the following (for example):

UIView *view = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, 50, 50}];

[array1 addObject:[NSValue value:&view->frame withObjCType:@encode(CGRect)]];

But this is causing warning: &view->frame

It is a property, but I would like to access it directly so that I can use the above syntax and use it for the value: parameter like the above.

I know I could access &self->_view (if I wanted to add the view to the array, which is not the case here), so I tried &view->frame (and even &view->_frame), but it is causing warning and doesn't work.

It would, unless I so the following first:

CGRect tempRect = view.frame; 
[array1 addObject:[NSValue value:&tempRect withObjCType:@encode(CGRect)]];

So my question is, what have I done wrong here with &view->frame (or even: &view->_frame)?

And is there a way to do that without first having to do this:

CGRect tempRect = view.frame;
[array1 addObject:[NSValue value:&view->frame withObjCType:@encode(CGRect)]];

Other words, can I simply use a cast here without having to create first a CGRect (tempRect)?

Addendum:

If my memory serves, I think casting on & in C is invalid. Am not sure, please let me know if my memory is correct.

Was it helpful?

Solution

[array1 addObject:[NSValue valueWithCGRect:view.frame]];

CGRect rect = [[array1 lastObject] CGRectValue];

Documentation: NSValue UIKit Additions Reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top